views:

315

answers:

6

I have a site which has an area that requires authentication. Right now I use the roles attribute on all the controllers in that area, and I run a query to retrieve that users ID, and all their settings.

It seems like a code or design smell to me that I am retrieving the userid and settings each time a controller in that area loads up? I'm not sure if I should be using sessions, or if ASP.Net MVC 2.0 provides some unique way to handle this. Another concern is security.

Overall, I don't really know which way to turn. Design wise I would like the userId and settings retrieved only once when the user logs into the area. Right now I grab the userId each time a controller loads up, and then if required, I query the database for their settings each time as well.

A: 

You could store an object in session that holds all the required user information. You will just need to add a property in the Controllers, Views or other base classes where you want to retrieve the user information/profile. This would be the authorisation info as opposed to any authentication info (eg Forms authentication)

Mark Redman
I'm on the fence about using sessions. Some people say you should never use them and others say they are ok to use.
chobo
If you are careful about the amount of storage used for each user it should fine. Cookies are an option, but I would explore web/mobile browser restrictions.
Mark Redman
+1  A: 

You could also implement a custom identity. They are very easy to implement, and they let you store whatever user information you want in Identity, which is then stored in the cookies that Identity puts down, so you're not hitting the DB every time to get that info.

Just create a new class that inherits from GenericIdentity, and you'll be on your way.

You of course have to be careful how much info you put there since it's in a cookie, but usually user related information in the case you're talking about here isn't so big.

We use a custom identity to store a few bits of info about the user, and it works out pretty well.

CubanX
I think for setting information a cookie might be a good choice to go.
chobo
+9  A: 

One of the rules about security is that you shouldn't try to do it yourself. There are many pitfalls in doing an authentication system correctly without leaving loopholes or backdoors. Thus, in that regard, you might consider the SqlMembershipProvider that comes with .NET. It can be used with MVC and provides the means to get roles and the current security context, is easy to setup and configure and will be more secure than rolling your own.

If you are not using SQL Server, you have a couple of choices. One solution would be to use something like SQL Server Express or SQL Server Compact Edition to maintain the credentials. Another solution would be to mimic the SqlMembrershipProvider database schema and then write a custom provider that communicates with that schema.

The last choice would be to write a custom MembershipProvider class. While this is still rolling your own, it forces you into the structure of the MembershipProvider so that you can swap it out at a later date for a different one (e.g. ActiveDirectoryMembershipProvider) and provides a common interface for interacting with credentials and logins which for example enables easy use of the built-in Login control.

If you are already using a MembershipProvider and are asking about storing additional user-specific data, then I would suggest the SqlProfileProvider with all the caveats I mentioned above about the SqlMembershipProvider. the ProfileProvider provides a structure for maintain user-specific data with the currently logged on user.

For more information:

Thomas
I'm currently using a custom membership provider that works well. What I am tryng to figure out is what would be the best way to handle the userId and settings.I would like to only have to retrieve the userId and settings once when the person logs into the area. I'm just not sure which way would be the best to persist that data across otehr controllers in that area. Also taking into account performance and security.
chobo
@chobo - If you are using a custom MembershipProvider, then you should be able to use the `ProviderUserKey` property on the `MembershipUser` class which is returned from GetUser to get the UserId. For user settings, I would suggest looking at implementing a ProfileProvider.
Thomas
@chobo: I would not recommending using the ProfileProvider, its an overhead you dont need and the way its implemented out the box is terrible. Its much easier to add settings and preferences in the users table (or associated table) and create a small DTO/ViewData object saved in session that holds often used info (not authentication info) which is handled by your MembershipProvider.
Mark Redman
I would also recommend reading the excellent article by 4GuysFromRollahttp://www.4guysfromrolla.com/articles/120705-1.aspx
Andrew Garrison
A: 

You might try "Windows Identity Foundation". I've been using it on one of my projects for a while. It allows for "claims-based authentication", which basically means that you get to designate "claims", strings of information that describe the user when she logs on.

Once logged on, the user's claims can be read from the HttpContext.Current.User field. You can also use "Role" claims that seamlessly integrate with a role-based authentication schema; meaning that you can give the user a "manager" role claim and then use `if (User.IsInRole("manager")).

As an added bonus, WIF makes it very easy to re-use your login screen in other applications.

All in all, it's very flexible, but the documentation is very poor. I've asked and answered a number of questions about "Windows Identity Foundation" on StackOverflow.

Rice Flour Cookies
A: 

We have done this quite a few times in the past. Similar to what Thomas mentions, what we have generally done is implemented a new Membership provider based on the Microsoft SQL Memberhsip provider to do this. We inherit from the base MembershipUser class and add any custom properties we would want to have on the user object. You have to implement a database read for the Membership provider on the GetUser implementation, so you can consolidate your extra properties you need into that database read.

If you are using SQL server, Microsoft has release the 2.0 code for it. You can get more information at Scott Gu's blog.

http://weblogs.asp.net/scottgu/archive/2006/04/13/442772.aspx

If you want to start from scratch, they also have good resources at MSDN.

http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx

and

http://msdn.microsoft.com/en-us/library/6tc47t75.aspx

Once you have implemented your provider, you can then add the Membership user to the Items collection of the current web context to get access to it from your code. The non extended properties from the base base user class are also available on the Request thread like normal.

With the Microsoft release of the 2.0 version of the source code , we found it helped us alleviate some concerns that exist about reinventing. Another thing to consider for your implementations is based on your scenario, you can bypass implementing some of the code. An example of this would be the CreateUser code if you are hitting a back end system that already has the credential information.

John Ptacek
A: 

It seems like you're relatively happy with your authentication process but you want to explore other options for session/settings.

My suggestion has to do with settings only (roles, preferences, etc.)

In my opinion, having to traverse the whole technology stack from UI to Business Tier to DB tier to DB is sometimes a bit overkill. For data that isn't likely to change during a session, this adds a lot of overhead... There are potentially several data transformations happening (DB (Relational Format) -> ORM -> Web Service XML Serialization -> Web Tier deserialization).

You might consider a session system that doesn't rely on a heavy RDBMS system or on the ASP.NET Caching / Session model. There are options that are very performant and that scale well.

You could use RavenDB by Ayende Rahien (Built for .NET). Its main goal is to provide low latency, high performance access to schema-less JSON documents.

Using this solution, you would set up ravenDB in the web tier so that access to data is very quick. The first time you authenticate and retrieve settings, you would store the userID and settings information in this session DB. Every time you load your controller after that, the settings data is accessible without having to go back to the RDBMS. This DB could also be used to cache other web related data.

As for security, the settings data makes it to the web tier regardless of the method you use. This solution would be no more or less secure than the other options (more secure than an unencrypted cookie). If you needed to, you could encrypt the session data - but that will increase your overhead again.

Just another one of the million options to consider.

Good Luck,

Let us know what you decide!

Patrick.

Patrick
I am happy with the authentication. I thought maybe there would be a simple way or technique in asp.net mvc framework for persisting user settings without having the need to use sessions. For some reason I thought with mvc you don't need sessions or there were some other techniques for this.
chobo