简体   繁体   中英

Custom Attribute with optional parameter

I have this Custom Attribute (Custom MVC Authorization):

public class CustomAuthorizeAttribute : AuthorizationFilterAttribute
{
    public string Users { get; set; } //its always null!

    public override void OnAuthorization(HttpActionContext actionContext)
    {
        string user = Thread.CurrentPrincipal.Identity.Name.Split('\\')[1];

        AdProxy AdProxy = new AdProxy();

        if (!AdProxy.IsUserInGroup(user, Users))
        {
            actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);

        }
    }

}

I use it like this:

[CustomAuthorizeAttribute(Users = "Admin")]

But on debugging the value of "Users" is always null. Any idea?

If you are using .net Framework 4.5.1 change to 4.5 and it should work.

class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public string Users { get; set; }
    public override void OnAuthorization(AuthorizationContext filterContext)
    {

        base.OnAuthorization(filterContext);
    }
}

Try this:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
 { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM