Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute ASP.NET Membership Login from codebehind in button click handler

I'm trying to seamlessly log in the user without prompting for credentials as part of a <asp:Wizard> process. My strategy is to handle the NextButtonClick event and login the user in code. I already have the user's credentials saved in session variables.

Is it possible to login a user in code? Will a hidden <asp:Login> control behind the scenes be required?

like image 549
ChessWhiz Avatar asked Dec 29 '22 05:12

ChessWhiz


2 Answers

If you're storing their credentials in session, I hope you are encrypting them.

But yes, if you have their credentials already, you can do:

FormsAuthentication.SetAuthCookie(username, true);

You can also run:

if(Membership.ValidateUser(username, password)) {
     FormsAuthentication.SetAuthCookie(username, true);
}

before hand to make sure that you have the correct username and password.

like image 103
Jack Marchetti Avatar answered Feb 05 '23 23:02

Jack Marchetti


// This will redirect the user (check Jack Marchetti's answer for other option)
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);

This will issue the authentication ticket for the user

like image 35
Yona Avatar answered Feb 05 '23 21:02

Yona