I’m currently creating an application using the MVC3 framework. I understand how to use roles with filters like:
[Authorize(Roles = "Admin")]
My question is:
Where do I set roles? Is it on login? How is this achieved?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When creating a Forms Authentication ticket on your own you would generally use the UserData portion of your ticket to store information related to your user. This could be the roles.
Then in the Global.asax on the Application_AuthenticateRequest event you would parse your Forms Ticket and assign the roles to the current security principal.
Here are some guides on Forms Auth with different providers:
http://weblogs.asp.net/scottgu/archive/2006/02/24/ASP.NET-2.0-Membership_2C00_-Roles_2C00_-Forms-Authentication_2C00_-and-Security-Resources-.aspx
In general I usualy write my own System.Security.Principal.GenericPrincipal and System.Web.Security.FormsIdentity to do all the work for me.
And in your Global.asax:
And to write the ticket:
The code might be hard to follow but the logic is that the custom “UserPrincipal” will auto-parse the UserData section of the Forms Auth ticket for what ever information you want to store there. In my case I’m storing name, roles, id, etc. In my code the namespace “CAA.Utility.Security” is where my custom Identity and Principal are stored.