Guy Z
Guy Z

Reputation: 693

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?

Upvotes: 2

Views: 1236

Answers (2)

Ognyan Dimitrov
Ognyan Dimitrov

Reputation: 6251

Try this:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
 { ... }

Upvotes: 0

Narek
Narek

Reputation: 449

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);
    }
}

Upvotes: 1

Related Questions