2016-09-21 7 views
0

역할을 유지하려고하고 권한 기반 인증을 얻으려고합니다. ASP.Net MVC : 사용자 역할이 제공된 역할과 대조되는 위치

그냥 http://www.dotnetcurry.com/aspnet-mvc/1102/aspnet-mvc-role-based-security

는 사용자 로그인을 가정하고 그 판매 역할을하지만 행동 자신이 관리 역할에 대한 액세스를 시도하지만 난 사용자 역할 및 공급 역할이 사용자를 알고 비교하는 어떤 코드를 발견이 기사를 읽는 것은 관리자 역할을 아닙니다. 이 지역은

private void IsUserAuthorized(AuthorizationContext filterContext) 
     { 
      // If the Result returns null then the user is Authorized 
      if (filterContext.Result == null) 
       return; 

      //If the user is Un-Authorized then Navigate to Auth Failed View 
      if (filterContext.HttpContext.User.Identity.IsAuthenticated) 
      { 

       // var result = new ViewResult { ViewName = View }; 
       var vr = new ViewResult(); 
       vr.ViewName = View; 

       ViewDataDictionary dict = new ViewDataDictionary(); 
       dict.Add("Message", "Sorry you are not Authorized to Perform this Action"); 

       vr.ViewData = dict; 

       var result = vr; 

       filterContext.Result = result; 
      } 
     } 

[AuthLog(Roles = "Manager")] 
public ActionResult Create() 
{ 
    var Product = new ProductMaster(); 
    return View(Product); 
} 

[AuthLog(Roles = "Sales Executive")] 
public ActionResult SaleProduct() 
{ 
    ViewBag.Message = "This View is designed for the Sales Executive to Sale Product."; 
    return View(); 
} 

이 역할 "판매 집행"과 사용자 1이 코드에서 USER1 어떤 역할 호출하지 않았다 관리자 비교입니다 다음 행동 만들기에 액세스하려고 가정

참조? 어떻게 된 일입니까? 누가 커튼 뒤에서 일을하고 있니?

사용자 1은 관리자 역할을 가지고 있지 않습니까? 인 코드로이 확인이 수행 된 경우에만이 점에 유의하시기 바랍니다.

감사

자신의 AuthLogAttributeOnAuthorization에 다음 AuthorizeAttribute에서 inhereting 것을

답변

1

공지 사항, 그들은 단순히 전화 후 AuthorizeAttribute.OnAuthorization(filterContext);

public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    base.OnAuthorization(filterContext); 
    IsUserAuthorized(filterContext); 
} 

를 호출, 그들은 다음 있는지 확인 자신의 IsUserAuthorized() 전화 Result 속성이 filterContext 인 경우 AuthorizeAttribute으로 설정했는지 여부를 확인합니다. null가 아니고 사용자가 인증되면 이는 사용자에게 필요한 역할이 없음을 의미합니다.

"user1은 (는) 코드에서 Manager라는 역할이 없습니다."라는 질문에 대답하려면 기본 코드는 AuthorizeAttribute입니다.

AuthLogAttribute에 약간의 가치가 있다고 개인적으로 생각합니다. 사용자가 필수 역할을 수행하지 않은 경우 이동할보기와 함께 기본 오류 메시지를 지정하기 때문입니다. 대신 표준 AuthorizeAttribute을 사용하는 데 전념 할 것입니다.

+0

죄송합니다. 시스템 1이 시스템 1을 감지하는 방법이 관리자라는 역할을 가지고 있는지 여부는 아직 명확하지 않습니다. user1에 manager라는 역할이없는 경우 오류 페이지로 리디렉션합니다. 가능한 경우 작은 작업 코드를 게시하십시오. –

+0

AuthorizeAttribute의 소스 코드를 살펴보십시오. 특히 102 행 : https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/AuthorizeAttribute.cs#L102 사용자가 속성에 전달 된 역할은 –

+0

링크에 감사하지만 아래 라인은 명확하지 않습니다. if (_usersSplit.Length> 0 &&! _usersSplit.Contains (user.Identity.Name, StringComparer.OrdinalIgnoreCase)) { false false; } if (_rolesSplit.Length> 0 &&! _rolesSplit.Any (user.IsInRole)) { false false; }'이해하도록 도와주세요. –