getting Roles that already set to custom Authorization attribute?

98 views Asked by At

I customized the authorize attribute of Asp.Net but I do not know how to get the roles which I set to the attribute when I set the attribute to a method or class

For example I have this CustomeAuthorizeAttribute

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class CustomeAuthorizeAttribute : AuthorizeAttribute
{

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {


        if (HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.IsInRole("Super"))
        {
            return true;
        }
        else
            return false;
    }
}

but I do not know how to get the roles when I set them to the attribute like this

[CustomeAuthorizeAttribute(Roles="admin,super-admin")]

1

There are 1 answers

0
Mo Haidar On BEST ANSWER

by default it inhirits the Roles property from the base Authorize class so you can get the roles directly by using the Roles property

For Example

if (HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.IsInRole(Roles))
    {
        return true;
    }

or you create new properties belong to your custom Authorization attribute and use them.