views:

129

answers:

3

Hi

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.

+1  A: 

You can simply create your own authorization scheme by implementing your custom attribute and decorating controller actions with it, if you need.

One thing to be aware of is caching. If you implement authorization in a straightforward way and then enable caching on some internal (account-only) method, then the lastly generated action output will be served to anyone asking for it (for the specified duration of caching).

We had a couple of days ago a very similar discussion: Is it possible to create a Logon System with ASP.NET MVC but not use the MembershipProvider?

Developer Art
Hi I am not following everything you said. Are you saying that if I ditch asp.net membership I have to make my own Authorize Tag and Roles tag? I can't use the built in stuff anymore? Is really that dependent on asp.net membership?
chobo2
No, you can implement your own membership provider and use with those attributes.
Developer Art
Ok that is good I would not want to roll that stuff out.
chobo2
+1  A: 

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.

Pete
I have a login already and on creation I use FormsAuthentication.SetAuthCookie(...,...). I am not sure how it stores the cookie or what. Is there any tutorials on rolling out your own stuff I am just sort of worried I will foreget something and have major flaw in my site.
chobo2
+1  A: 

All you need is upon successful login to create the cookie:

FormsAuthentication.SetAuthCookie("username", false);

And when you need to sign out just use:

FormsAuthentication.SignOut();

User.Identity.Name will work because it looks for the authentication cookie and if it is sent along with the request it will decrypt it and read the username which is stored inside. You could use anything for user authentication and authorization.

Darin Dimitrov
Will the cookie stuff be generated from what is in the webconfig?
chobo2
Yes, FormsAuthentication uses the "authentication" section in web.config to setup the cookie.
Darin Dimitrov
So basically all the asp.net membership does is just takes this? So the asp.net membership just has stuff to make it easier to add a user and stuff? It has nothing to do with keeping track of the user and setting the actual cookies(it passes that stuff to the Formsauth?)
chobo2
ASP.NET default Membership provider allows you to easily create, modify, delete users stored in SQL Server tables. The authentication and cookie management is done through the FormsAuthentication class.
Darin Dimitrov