2

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?

2
  • First I think that you forget that Users in your class is not base.Users. Probably you are missing the constructor with optional parameter which overrides the base.Users property or just fill it through your parent constructor. Commented Jul 1, 2015 at 14:09
  • What do you mean? can you write the ctor plz? According to this link: dotnet-tricks.com/Tutorial/mvc/… I dont have to add a ctor and fill the parameters Commented Jul 1, 2015 at 14:13

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
 { ... }

1 Comment

It should work now. There was misspell in AuthorizeAttribute - There is no AuthorizationFilterAttribute. Only Authorize attribute which contains in itself Users property.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.