1gn1ter
1gn1ter

Reputation: 1444

Custom Role Membership Provider: how to set specific Roles to the user in order to use HttpContext.User.IsInRole method?

I'm implementing custom Authorization with OpenId. In the database I have User Name (unique), OpenId (unique) and set of User Roles.

While the user is being Authorized, I set

//GetRolesFromTheDBAndAssignThemToTheUser(); how?
FormsAuthentication.SetAuthCookie(GetUserName(OpenId), false);

And using custom attribute on controller:

 public override void OnAuthorization(AuthorizationContext filterContext)
        {
           //..some code

           var user = filterContext.HttpContext.User; //I can get user 
           var roles = Roles; //I can get roles
           var isAlowed = roles.Split(',').Any(user.IsInRole); //validate if user is alowed to use the current page or do other stuff
        }

But how I can set specific Roles to the user in order to use HttpContext.User.IsInRole method?

Upvotes: 1

Views: 643

Answers (1)

Wim
Wim

Reputation: 1985

You can create new GenericPrincipal(identity, roles) object and assign this to the HttpContext.User in the Application_AuthenticateRequest function in the global.asax. I have described this in my blog post. You can use an other IIdentity object.

Upvotes: 1

Related Questions