forms authentication - How do I implement custom Principal and Identity in ASP.NET MVC? -


I want to store additional information in authenticated user so that I can easily access it (like User.Identity.Id, for example ), Rather than just the name, because I'm planning to be that non-unique.

So far I have collected that I should implement custom principals and / or identities, but I am not sure how to know about this I am looking for documentation and tutorials on this matter , But I have got related content in different places and I have confused it a little bit.

I have seen how to add custom information to the authentication cookie in user data property, but I want to take advantage of dependency injection for unit testing, which can be with my principal and identity.

Precise Step I need to consider whether I want to implement my own Principal or identity?

What can I do to simplify this scenario (just add the ID and keep all the omissions in place)? "Default" will include the default providers (subscriptions, roles, etc.).

I have seen, but I appreciate those answers which do not leave any holes in the middle, such as the roles in the authentication incident in the examples magic string Instead, I need to know how to add roles to the current user with the default SqlRoleProvider: when and where to do, and if I need to do something else to add my new classes with other default providers

It would be nice to be able to go to this sample ASP.NET MVC 2 application (from the Visual Studio 2010 template, for example), edit and work it. Edit: I've better edited to show that I have been here too many lost < / Strong>, so I can not do with a very high level of answers

PS: I think it is more to keep the ID in the identity than the principal Makes sense, though, I have said it earlier. / P>

Before this question has been asked and answered:

but summarized In the ...

Crop a custom principal cloth with additional custom, which you would like to store:

  Public class CustomPininsel Inherits system. Security Principal. Read Generics Principal Private _AE Color String Public, Get String Just like Property I Correlate _eyeColor End End Property Public Sub New (ID System Security. Principal. Roles in the form of identities, strings, as the color of the eye color ) MySQL (id, roles) _Eye color = eye color end sub and class  

use your custom principal to gl Modify obal.asax Global.Application_AuthenticateRequest:

  Protected sub sub Ication_AuthenticateRequest (as the object, byVal e, the sub-sender as the system.AventErgues) ... string = {"Splplorol"} as ... dim roles. User = new custom prinipinal (contextual user identity, roles, "green") End Sub  

Then when you want to refer to any one of these properties, Also do this:

  CType (My.User.CurrentPrincipal, CustomPrincipal). Eye Color  

Comments