How to assign a role to an OpenId user for an ASP.NET MVC site?

I am using OpenId in my ASP.NET MVC application. Works great :) After I have an OpenId user ID (as soon as they are authenticated and returned to my site), I upload user data (to get a display name, etc.).

From here I also know their roles.

I am not sure how to assign a role to the current Forms.Identity.

here is my code ...

// Load User... var user = GetUsers().ByOpenIdIdentifier("blahblahblahbl...."); // Here means we have a user AND all the roles, for that user. // Forms Authenticate and Redirect. FormsAuthentication.SetAuthCookie(user.DisplayName, true); return RedirectToAction("Index", "Home"); 

How can I change this code so that the authenticated user is assigned their roles?

Update

I came across this web post about creating a custom Authorize attribute. Notice how they check the role of registered users in the session? In addition, the roles are enumeration :) It's quite funny if you ask me :) It's nice and simple.

Thoughts (compared to the full RoleProvider class?)

+6
authorization asp.net-mvc dotnetopenauth roles
source share
2 answers

You need to write your own RoleProvider and include it in the web.config file. Your RoleProvider will accept the username and find out their role (s). IPrincipal.IsInRole uses the configured RoleProvider to determine role membership.

+4
source share

Take a look in this article.

It shows an easy way to integrate openid with roles and membership profile. Hope can help.

+1
source share

All Articles