简体   繁体   English

带有可选参数的自定义属性

[英]Custom Attribute with optional parameter

I have this Custom Attribute (Custom MVC Authorization):我有这个自定义属性(自定义 MVC 授权):

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.如果您使用 .net Framework 4.5.1 更改为 4.5,它应该可以工作。

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

        base.OnAuthorization(filterContext);
    }
}

Try this:尝试这个:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
 { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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