views:

53

answers:

3

I'm experimenting with writing my own custom MembershipProvider in asp.net and I want to roll my own login page. We do some fairly special stuff at login time so we can't use the default login control so I need a way to manually log a user in.

So far I haven't found anything on how to write your own login control so I'm here, wondering how I can manually log a user in via a MembershipProvider.

I've tried

Membership.ValidateUser("user", "pass");

and while that does call ValidateUser() on my custom MembershipProvider, and it does return true, it doesn't actually log me in.

Btw I'm fairly new to the whole MembershipProvider stuff so if I'm not even on the right wavelength, feel free to let me know.

+4  A: 
if (Membership.ValidateUser(Username.Text, Password.Text))
{

   FormsAuthentication.SetAuthCookie(Username.Text, false);
   FormsAuthentication.RedirectFromLoginPage(Username.Text, false);
}
else
{
// do something else
}

The above was copied from working code with a custom membership provider in a situation just like yours, where we needed to do a bunch of extra work at login. (Sensitive operations reomved to protect the innocent.)

David Stratton
+4  A: 

A MembershipProvider only stores the data about the users, actual logins and session handling are handled by an AuthenticationProvider. For example, if you're using forms (=cookie based) authentication, check out FormsAuthencation.SetAuthCookie and the other related methods in that class.

Matti Virkkunen
ahh thanks for explaining the separation for me! This is really what I needed
Allen
yep, AuthenticationProvider wasn't even on my radar, this is going to help alot
Allen
Remember that the built-in forms authentication provider works quite fine with custom membership providers, and unless you need to do something very special, it should "just work".
Matti Virkkunen
Awesome, thanks. Though I do appreciate it just working, I like to understand how everything works and fits together so I can be aware of everything that is going on. Plus its easier to explain to management this way
Allen
I can understand that. I find myself looking at the source of .NET using Reflector more and more often nowadays, just because I want to know how they do stuff.
Matti Virkkunen
+1  A: 

For future reference, you can check out the internal code of any component if you look inside the assemblies with Reflector:

There is a free version which will do everything you need.

Also you shouldn't have to create your own Login control because the Login is very extensible. You can edit the template and handle various events to get it to do what you want.

rtpHarry