0

권한 부여 특성을 재정의해야합니다.인증 특성 무시

기본적으로 아약스 요청 및 사용자가 로그인되어 있지 않거나 지정된 역할이 아닌 경우 JSON을 반환하려고합니다. JSON은 발신자에게 이유가 로그인하지 않았거나 역할에없는 것으로 알리고 URL로 리디렉션을 반환해야합니다. 서명하지 않은 경우 ReturnUrl을 돌려줘야합니다. 그되지 아약스 요청이 다음 내가 원하는 경우

권한을 부여하여 기본 처리에 걷어차 때문이다. 우리는 폼 인증 및 web.config 파일에 지정된 URL과 오류 페이지에있는 기호를 사용하는

. 다음

가에 내 걸릴 것입니다하지만 난하지 Ajax 요청의 경우 Ajax 요청

  • 의 경우 다음과 같은 권리

    1. 없는 역할 처리를 받고 있지 않다 (다른 블록) , 사용자를 로그인 페이지로 리디렉션하고 있습니다. 내가

    난 그냥 올바른 방향으로 ... 튜토리얼 또는 블로그 포인터의 밀어를 필요로이 경우 라덴에 대한 기본 autorize 속성을 원하는 내가 ....

    을 배우고이를 위해 필요한 모든 것입니다
    public class AuthorizePartnerProgramsAttribute : AuthorizeAttribute 
        { 
         public override void OnAuthorization(AuthorizationContext filterContext) 
         { 
          HttpContext httpContext = HttpContext.Current; 
    
    
          var url = new UrlHelper(filterContext.RequestContext); 
    
          var request = filterContext.HttpContext.Request; 
    
          if (request.IsAuthenticated == false) 
          { 
    
           if (filterContext.HttpContext.Request.IsAjaxRequest()) 
           { 
            if (request.Url != null) 
             filterContext.Result = CommonUtilities.AddJsonUtf8Encoding(new JsonResult { Data = new { error = true, singinerror = true, message = "Sign in required!", returnUrl = request.UrlReferrer.AbsolutePath.ToString() } }); 
            else 
             filterContext.Result = CommonUtilities.AddJsonUtf8Encoding(new JsonResult { Data = new { error = true, singinerror = true, message = "Sign in required!" } }); 
           } 
           else 
           { 
            if (request.UrlReferrer != null) 
            { 
             filterContext.Result = new RedirectResult(url.Action("Index", "SignIn", new { Area = "Account", ReturnUrl = filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath.ToString() })); 
            } 
            else 
            { 
             filterContext.Result = new RedirectResult(url.Action("Index", "SignIn", new { Area = "Account"})); 
    
            } 
           } 
    
          } 
    
         } 
    
    
        } 
    

    여기 내 두 번째 찌르기입니다. 나는 내가 이전보다 더욱 혼란 스러워요와 필요를 설정하는 데 도움이 생각 제대로

    public class AuthorizeCustomAttribute : AuthorizeAttribute 
        { 
         protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
         { 
          var request = filterContext.RequestContext.HttpContext.Request; 
    
          if (request.IsAjaxRequest()) 
          { 
           var url = new UrlHelper(filterContext.RequestContext); 
           var urlReferer = request.UrlReferrer != null 
            ? request.UrlReferrer.ToString() 
            : String.Empty; 
           var signInUrl = url.Action("Index", "SignIn", new { Area = "Account", ReturnUrl = urlReferer }); 
           var accessDeniedUrl = url.Action("PageAccessDenied", "Error", new { Area = "" }); 
           if (!request.IsAuthenticated) 
           { 
            //not authenticated 
            filterContext.Result = 
             CommonUtilities.AddJsonUtf8Encoding(new JsonResult 
             { 
              Data = 
               new {error = true, singinerror = true, message = "Sign in required!", url = signInUrl}, 
              JsonRequestBehavior = JsonRequestBehavior.AllowGet 
             }); 
           } 
          } 
          else 
          { 
           base.HandleUnauthorizedRequest(filterContext); 
          } 
         } 
    
         protected override bool AuthorizeCore(HttpContextBase httpContext) 
         { 
          if (httpContext.Request.IsAjaxRequest()) 
          { 
           //Use [AuthorizeCustom(Roles="MyRole1,MyRole2")] 
           //or [AuthorizeCustom] 
           //roles may not have been applied here 
    
           //checking authentication will be done by the HandleUnauthorizedRequest????? 
           //if no roles are specified then it is true = so give access to the resource 
           //user may have multiple roles or single role assigned, check and if not in role then return json back. 
           //.... 
          } 
          else 
          { 
           return base.AuthorizeCore(httpContext); 
          } 
         } 
        } 
    
  • 답변

    0

    이에 대한 전체 작업의 속성입니다 광산에게 여기 http://www.dotnet-tricks.com/Tutorial/mvc/G54G220114-Custom-Authentication-and-Authorization-in-ASP.NET-MVC.html

    사용

    [AuthorizeCustom(Roles = RoleNames.Admin)] 
    

    을 설정하는 나에게 도움이 어떤 정리도없이.

    public class AuthorizeCustomAttribute : AuthorizeAttribute 
        { 
         #region CONSTANTS 
    
         public const string SectionStemFuture = "StemFuture"; 
    
         #endregion 
    
    
         #region PROPERTIES 
    
         private string Section { get; set; } 
    
         #endregion 
    
         #region Constructor 
    
         public AuthorizeCustomAttribute() 
         { 
          Section = String.Empty; 
         } 
    
         public AuthorizeCustomAttribute(string section) 
         { 
          Section = section; 
         } 
    
         #endregion 
    
         #region Overrides 
    
         public override void OnAuthorization(AuthorizationContext filterContext) 
         { 
          var request = filterContext.HttpContext.Request; 
    
          var url = new UrlHelper(filterContext.RequestContext); 
    
          /* 
          var urlReferer = request.UrlReferrer != null 
           ? request.UrlReferrer.ToString() 
           : String.Empty; 
          */ 
          var urlReferer = request.Url.PathAndQuery; 
    
          var signInUrl = url.Action("Index", "SignIn", new { Area = "Account", ReturnUrl = urlReferer }); 
          var accessDeniedUrl = url.Action("PageAccessDenied", "Error", new { Area = "" }); 
    
          //overwrite the default sign in URL according to the section 
          if (!String.IsNullOrWhiteSpace(Section)) 
          { 
           switch (Section) 
           { 
            case SectionStemFuture: 
             signInUrl = url.Action("Index", "StemFutureHome", new { Area = "StemFuture", ReturnUrl = urlReferer }); 
             break; 
           } 
          } 
    
    
          if (!request.IsAuthenticated) 
          { 
           //not authenticated 
           if (request.IsAjaxRequest()) 
           { 
            filterContext.Result = 
             CommonUtilities.AddJsonUtf8Encoding(new JsonResult 
             { 
              Data = 
               new {error = true, signinerror = true, message = "Sign in required", url = signInUrl}, 
              JsonRequestBehavior = JsonRequestBehavior.AllowGet 
             }); 
           } 
           else 
           { 
            //this is not an ajax request 
            if (!String.IsNullOrWhiteSpace(Section)) 
            { 
             filterContext.Result = new RedirectResult(signInUrl); 
            } 
            else 
            { 
             //let the base authorization take care of it 
             base.OnAuthorization(filterContext);  
            } 
           } 
    
          } 
          else if (!String.IsNullOrWhiteSpace(base.Roles)) 
          { 
           var isRoleError = true; 
           var rolesAllowed = base.Roles.Split(','); 
           //authenticated and we have some roles to check against 
           var user = filterContext.HttpContext.User; 
           if (user != null && rolesAllowed.Any()) 
           { 
            foreach (var role in rolesAllowed) 
            { 
             if (user.IsInRole(role)) 
             { 
              isRoleError = false; 
             } 
            } 
           } 
    
           if (isRoleError) 
           { 
            if (request.IsAjaxRequest()) 
            { 
             filterContext.Result = 
              CommonUtilities.AddJsonUtf8Encoding(new JsonResult 
              { 
               Data = 
                new 
                { 
                 error = true, 
                 signinerror = true, 
                 message = "Access denied", 
                 url = accessDeniedUrl 
                }, 
               JsonRequestBehavior = JsonRequestBehavior.AllowGet 
              }); 
            } 
            else 
            { 
             //here we will need to pass to the access denied 
             filterContext.Result = new RedirectResult(accessDeniedUrl); 
            } 
           } 
    
          } 
    
         } 
    
         #endregion 
        }