By Billy Logan


2009-11-30 21:33:35 8 Comments

I am upgrading a site to use MVC and I am looking for the best way to set up Authentication.

At this point, I have the log-in working off of Active Directory: validating a username and password, and then setting the Auth cookie.

How do I store the user's role information at time of log-in, in order for my controllers to see those roles as the user navigates through the site?

[Authorize(Roles = "admin")]

I have no problem getting a list of roles from Active Directory. I just don't know where to put them so that the controllers will see them.

5 comments

@Klaus Byskov Pedersen 2009-12-01 09:43:42

When you authenticate your user, you generate a new GenericPrincipal instance. The constructor takes an array of strings which are the roles for the user. Now set HttpContext.Current.User equal to the generic principal and write the auth cookie, and that should do it.

@David Glenn 2009-12-01 14:10:14

Roles are added to the IPrincipal of the HttpContext. You can create a GenericPrincipal, parse the list of roles in the constructor and set it as HttpContext.User. The GenericPrincipal will then be accessible through User.IsInRole("role") or the [Authorize(Roles="role")] attribute

One way of doing this (in C#) is to add your roles as a comma separated string in the user data parameter when creating your authentication ticket

string roles = "Admin,Member";
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
  1,
  userId,  //user id
  DateTime.Now,
  DateTime.Now.AddMinutes(20),  // expiry
  false,  //do not remember
  roles, 
  "/");
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                                   FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);

Then access the role list from the authentication ticket and create a GenericPrincipal from your Global.asax.cs

protected void Application_AuthenticateRequest(Object sender, EventArgs e) {
  HttpCookie authCookie = 
                Context.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (authCookie != null) {
      FormsAuthenticationTicket authTicket = 
                                  FormsAuthentication.Decrypt(authCookie.Value);
      string[] roles = authTicket.UserData.Split(new Char[] { ',' });
      GenericPrincipal userPrincipal =
                       new GenericPrincipal(new GenericIdentity(authTicket.Name),roles);
      Context.User = userPrincipal;
    }
  }

@Billy Logan 2009-12-01 15:04:53

If Klaus hadn't answered well before you i would have given you the correct answer. This is the exact way i am doing things as well. Thanks for the detailed answer. Sorry it was a little to late.

@Omu 2010-02-17 20:16:56

I guess this is not going to work for users that have cookies disabled ?

@David Glenn 2010-02-17 22:27:45

@Omu You can support cookieless authentication by simply modifying the Application_AuthenticationRequest() method to look for the authentication ticket in the URL if the cookie is not present. The important part is that the roles are stored in the encrypted authentication ticket.

@Joshua Hayes 2010-11-22 22:39:00

If you store roles in a cookie like this, what happens if a user's role is removed by an administrator or something? You wouldn't see this update until the cookie expires or you clear the cookie cache?

@Shaul says I Support Monica 2011-03-15 16:20:29

this is a great answer, and I've tried to use it, but it's not working for me. Could you please see my question; maybe it'll be obvious to you what I'm doing wrong? stackoverflow.com/questions/5314673/…

@Scott Coates 2012-03-02 08:09:04

This event is invoked for each request (including images, css, etc). Would you want to put in a condition that only adds roles once?

@VAAA 2012-07-24 19:34:20

@DavidGlenn do I have to change FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); to FormsAuthenticationTicket stuff? Thanks

@jwaliszko 2013-09-07 16:18:12

I've tested it in ASP.NET MVC 4 and I suggest to use Application_PostAuthenticateRequest instead. Otherwise the generic principal will be overridden.

@Lev 2014-08-27 10:07:28

I always set Thread.CurrentPrincipal=userPrincipal with Context.User=UserPrincipal; otherwise User is null in some cases.

@Kevin Dark 2016-09-06 20:23:39

This is a OK approach, but why wouldn't you use the stored user's Id in authticket.Name to make a call to the database within the Application_PostAuthenticateRequest method to retrieve their roles and initiate the IPrincipal that way? I know it's two calls to the DB but it saves storing them in a cookie and prevents hacks.

@Gareth 2013-11-19 08:17:37

For those of you using MVC 4 or Greater you will need to take Jaroslaw Waliszko's advice when making use of David Glenn's answer:

"I've tested it in ASP.NET MVC 4 and I suggest to use Application_PostAuthenticateRequest instead. Otherwise the generic principal will be overridden." – Jaroslaw Waliszko Sep 7 at 16:18

So as stated above, all you need to do is replace the Application_AuthenticateRequest method name with Application_PostAuthenticateRequest to get this to work. Worked like a charm for me! If I was allowed to upvote Jaroslaw and David, I would.

@Gareth 2014-01-06 05:52:11

I tried but my Stack Overflow reputation is non-existent due to me being a newly registered member so I can't comment. If this goes through it would shock and surprise me :) Queue shock and surprise

@DanH 2011-07-07 00:15:15

I'd be inclined to just create a custom role provider. Example here:

http://www.danharman.net/2011/06/23/asp-net-mvc-3-custom-membership-provider-with-repository-injection/

@Zhaph - Ben Duguid 2009-12-01 10:24:01

Could you not drop in either an authorization store role manager or find (e.g. on Codeplex) or write another Role Provider that works with Active Directory to get the groups information?

This would save you the hassle of authenticating the user, getting their roles, and then re-passing that information into the constructor, and would all happen automatically for you as part of the framework.

Related Questions

Sponsored Content

5 Answered Questions

14 Answered Questions

[SOLVED] RESTful Authentication

14 Answered Questions

[SOLVED] PostgreSQL error: Fatal: role "username" does not exist

7 Answered Questions

1 Answered Questions

integrating forms authentication in to mvc4 site

3 Answered Questions

Sponsored Content