R – How to create a cookie for login? Or should I stick with asp.net membership

asp.net-membershipasp.net-mvcnet

I am using asp.net MVC with the asp.net membership but I starting to think that there is no point for me to use Asp.net membership.

So I would like to figure out how to generate the same type of cookie with the same fields and the one the asp.net membership one does.

Also is there any other settings I need? Like how about stuff like "User.Identity.Name" work? Like where does that get set? Does that grabbed from the cookie or what? Or is this from something else?

Same with "Page.User.Identity.Name".

Does asp.net membership set anything else that I am not aware of that could screw things up like the Authorize Tag in asp.net MVC?

I don't have much clue how login/authentication and that stuff works since I always used asp.net membership.


Read below why if you want to know why I am thinking of ditching asp.net membership


I like asp.net membership and its good to use if you don't seem to have to do many changes to it or you need something fast.

For my needs I don't think it really suits me. I know there is the provider model that you can use and override it all but maybe its just I don't understand(I really have not looked really too much into it) but I find it limiting too.

How I understand you can use it to override all the classes or something like that. But if your just overriding it, won't you have to use the same parameters and such? Like I guess you can do overloading and stuff but what about the ones that you can't use anymore there just floating around then empty?

Next even if you get by all this will I still have this problem? I allow duplicate userNames since I have easy ways to tell which user is which by looking at other fields. I need duplicate userNames otherwise I could see potential problems.

So because of these duplicate names I can't even load up the asp.net membership admin website since it comes back with a error about duplicate names.

Next I have what potentially could be considered 2 separate databases I don't want to separate databases since I want to keep costs low. So now I have 2 tables that derive from the asp.net users table. The first table stores users of a certain type and the other table stores other users of another type so if I get rid of the asp.net membership tables I can just have these as the base tables for both of these tables and if I ever decide to spit them apart it would be super easy.

So these reasons and the fact that I basically have my own method for everything I need(already written) and I am not using any built in method from asp.net membership, I think I should just drop it and be free from its constraints.

The Provider Model seems good if you just want to port the default database to another database such as mysql but otherwise I just don't see a point to it but this could be because I know little about it.

Best Answer

You can easily create your own authentication method. And I would always do so for serious web projects. The problem with using systems like Membership is that when you find that it lacks a feature, you have no way of implementing that feature.

The first thing you need to do is create a logon page. Here you implement the functionality of checking the user's username and password (or certificate, or however they can log on). Then you store a cookie, representing the username. AFAIK, asp.net membership stored an encrypted version of the user's usersname. That is IMHO not very secure, becuase a hacker would only need to know the machine key of the server in order to impersonate any user on the site. I personally use System.Security.Cryptography.RandonNumberGenerator to generate a long binary token for the user, which I store in a table along with the user ID. This token is then handed to the users in a cookie.

Now, you need to implement the Application_AuthenticateRequest event. Here you read the cookie, and then check if that cookie corresponds to a valid user. Is so, you set the HttpContext.Current.User property to a valid IPrincipal. There is a simple IPrincipal implementation in framework, GenericPrincipal. This object also contains the user's roles.

Everything that has to do with authentication and authirozation after that, only checks to HttpContext.Current.User. So that means that the Page.User is set to the value that you set. If you have directory level security set, i.e. perhaps only certain roles can access a specific folder in the web application, that will work, etc.

This is actually the old forms-authentication method from .net 1.0. Membership is just a layer build on top of forms authentication.