views:

265

answers:

1

Hi Folks,

I'm using OpenId in my ASP.NET MVC application. Works great :) Once i have the user's OpenId Identifier (once they have authenticated and returned to my site), i load up the users data (to get display name, etc).

From here, i also know their roles.

I'm not sure how to assign the role to the current Forms.Identity.

here's 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 the authenticated user also has their roles assigned?

Update

I stumbled across this web post about making a custom Authorize attribute. Notice how they are checking the logged in users role that exists in the session? Also, the roles are an enumeration :) This is pretty funky, if u ask me :) Nice and simple.

Thoughts (compared to a full on blown RoleProvider class?)

+3  A: 

You'll need to write your own RoleProvider and hook it up in the web.config file. Your RoleProvider will take the user's name and figure out their role(s). IPrincipal.IsInRole uses the configured RoleProvider to determine role membership.

tvanfosson
holy eeks! that seems like so much work :( I wsa hoping to just manually add the role to the IIdentity .. and it was going to be as simple as that :(
Pure.Krome
It's not that bad, especially if your role store is SQL. You might actually be able to use the existing ASP.NET SQL role provider or extend it. Even writing your own is not that bad. The number of methods, if I recall, may be daunting but they aren't particularly complex.
tvanfosson
I agree that the number of methods are daunting, but not complex. It's just the size of the thing, to only figure out if a user is Admin or Not. In my user table, i have a bit field. I'm not going to have multiple roles. This is why i'm a bit .. daunted.
Pure.Krome