I'm trying to add simple Authentication and Authorization to an ASP.NET MVC application.
I'm just trying to tack on some added functionality to the basic Forms Authentication (due to simplicity and custom database structure)
Assuming this is my database structure: User: username password role (ideally some enum. Strings if need be. Currently, user only has ONE role, but this might change)
High Level Problem: Given the above database structure, I would like to be able to do the following:
- Simple Login using Forms Authentication
- Decorate my actions with: [Authorize(Roles={ MyRoles.Admin, MyRoles.Member})]
- Use roles in my Views (to determine links to display in some partials)
Currently, all I'm really sure of is how to Authenticate. After that I'm lost. I'm not sure at which point do I grab the user role (login, every authorization?). Since my roles may not be strings, I'm not sure how they will fit in with the User.IsInRole().
Now, I'm asking here because I haven't found a "simple" accomplish what I need. I have seen multiple examples.
For Authentication:
- We have simple user validation that checks the database and "SetAuthCookie"
- Or we override the Membership provider and do this inside of ValidateUser In either of these, I'm not sure how to tack on my simple user Roles, so that they work with the: HttpContext.Current.User.IsInRole("Administrator") Furthermore, I'm not sure how to modify this to work with my enum values.
For Authorization, I've seen:
- Deriving AuthorizeAttribute and implementing AuthorizeCore OR OnAuthorization to handle roles?
- Implementing IPrincipal?
Any assistance would be greatly appreciated. However, I fear I may need a lot of detail, because none of what I've Googled seems to fit with what I need to do.
I think I've implemented something similar.
My solution, based on NerdDinner tutorial, is following.
When you sign the user in, add code like this:
Add following code to
Global.asax.cs
:After you've done this, you can use
[Authorize]
attribute in your controller action code:Please let me know if you have further questions.
I did something like this:
Assign the [Authorize] attribute to your controllers, you want to require authorization for
or to allow access, for example the Login and ValidateUser controllers use the below attribute
My Login Form
Login Controller and ValidateUser controller invoked from the Form post
Validate user is authentication via a WCF service that validates against the Windows AD Context local to the service, but you can change this to your own authentication mechanism
}
User is authenticated now create the new Identity
On my site at the the top of my _Layout.cshtml I have something like this
Then in the body
Add your users to the table "users in roles". Use the stored procedure "addusertorole" (something like that) in your code to add to various roles. You can create the roles very simply in the "roles" table.
Your tables to use: User, UsersInRole, Roles
Use the built in Stored Procs to manipulate those tables. Then all you have to do is add the attribute.
For example you can have an "Admin" attribute on a view that selects a user and adds them to a role. You can use the stored proc to add that user to the role.
Build a custom
AuthorizeAttribute
that can use your enums rather than strings. When you need to authorise, convert the enums into strings by appending the enum type name + the enum value and use theIsInRole
from there.To add roles into an authorised user you need to attach to the
HttpApplication
AuthenticateRequest
event something like the first code in http://www.eggheadcafe.com/articles/20020906.asp ( but invert the massively nested if statements into guard clauses!).You can round-trip the users roles in the forms auth cookie or grab them from the database each time.