Get ActionName, ControllerName and AreaName and pass it in ActionFilter Attribute

前端 未结 5 506
星月不相逢
星月不相逢 2021-01-31 08:34

I use a custom AuthorizationFilter like the followings:

public class ActionAuthorizeAttribute : AuthorizeAttribute {

protected override bool AuthorizeCore(Syste         


        
5条回答
  •  庸人自扰
    2021-01-31 09:10

    > namespace dene.kontroller {
    >     public class daAttribute: AuthorizeAttribute
    >     {
    >         private Entities db = new Entities();
    >         private readonly string[] allowedroles;
    >         public daAttribute(params string[] roles)
    >         {
    >             this.allowedroles = roles;
    >         }
    > 
    > 
    >         protected override bool AuthorizeCore(HttpContextBase httpContext)
    >         {
    >             bool authorize = false;
    >             foreach (var role in allowedroles)
    >             {
    >                 if (role == HttpContext.Current.User.Identity.Name)
    >                 {
    >                      
    >                     if (role!= null)
    >                     {
    >                         authorize = true;
    >                     }
    >                 }
    >                 
    > 
    >             }
    >             return authorize;
    >         }
    > 
    > 
    >         protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    >         {
    > 
    >             FormsAuthentication.SignOut();
    >             filterContext.Result = new HttpUnauthorizedResult();
    >         }
    > 
    >     } }
    

提交回复
热议问题