Take the 2-minute tour ×

I have a ASP.NET MVC site with a CAS server set up as the authentication type. I also have a separate database with a Users table and a Roles table (with a User being related to one or more roles). A User is only able to log into the system if the Username is both in the User table and on the CAS system. I have this solution working.

My problem is i now need some form of trigger on User.IsAuthenticated so i can track the current User (from my database), without the possibility that i am trying to allow tracking of a User that has logged out. What I've been thinking is i need to add the User to the HttpContext but i am not sure how to trigger the clearing of the User if the CAS session times out or if the User Logs out.

I also wish to have some functionality such as User.IsInRole (again using my database, not ASP.NET) but am not sure how to go about implementing this. I suppose if i can successfully add the User to the HttpContext then a IsInRole method would simply be a User.Roles.Contains(string role) method but how can that then be used if i wish, for example, to use a method with the DataAnnotation [Authorize(role = "ExampleRole")].

I have looked at questions such as How do I create a custom membership provider for ASP.NET MVC 2? but this doesn't work for me (possibly to do with me using the CAS authentication?)

Any guidance or background reading would be appreciated as i'm really not sure where i should even start. I have read up on GenericPrinciple, IPrinciple and IIdentity but I'm struggling to see how i can apply them to my current project.

share|improve this question
Have you looked at nerddinner? They implement a CAS, and use the FormsAuthenticationTicket to store details outside of the standard membership provider. –  Brad Christie Feb 29 '12 at 4:03
@Brad Thanks for the link, interesting how much nerddinner has changed since I last saw it. Unfortunately, I can't really see a way for the code there to help me, for a start my authentication code is completely different (I'm using Jasig-CAS not sure if that makes a difference) and also, from what I can tell, the only data the FormsAuthenticationTicket is storing is a userName and a expiry time (I can't provide a expiry time as the Jasig-CAS server is not controlled by me and I'm not sure what the expiry time is, it can also be renewed from other sites) whereas I need to store a class –  Manatherin Feb 29 '12 at 7:13
add comment

2 Answers

up vote 0 down vote accepted

Ended up with a custom Authorise Attribute that uses the CAS logon to check the user exists in my database. It also checks the roles of that user. I also used a static class to save the current user in the session with a logout method that abandons the session when the user logs out.

share|improve this answer
add comment

I have kind of a two parter for you. This link does a really good job of explaining how to replace the HttpContext User with your own object: http://bradygaster.com/custom-authentication-with-mvc-3.0

His approach uses MVC filters, but you can also catch the Authentication event in the Global.asax file. Using the forms system with your own implementation can be trivial or not depending on what you're doing, but it boils down to calling FormsAuthentication.SetAuthCookie and .SignOut, amidst your own logic.

   public static void FormsLogin(this User user, bool persist)
        FormsAuthentication.SetAuthCookie(user.DisplayName, persist);
        user.AddHistory("Login event.", HistoryType.Login, "SYSTEM");

    public static void FormsLogout(this User user)

Lastly, once you've got the login stuff working out, you can use your own more complex permission system by making a custom Auth Attribute. I remember piecing this together from some other answers and articles but I can't seem to find the sources at the moment, I will try and edit with sources for credit where it's due, if I find them. For now, all I can offer is this gist which offers up one of the attributes I use: https://gist.github.com/1959509

Keep in mind the only really relevant part there is the override of OnAuthorization, which does the actual work.

share|improve this answer
add comment

Your Answer


