Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User.Identity.IsAuthenticated false until redirected from Login Page

I have a login page that uses ASP.NET membership. Once I've validated the user I need to store some details about them in Session variables. The problem is that

Membership.GetUser() == null and 
User.Identity.IsAuthenticated == false 

until I have navigated away from the page.

I have tried using

FormsAuthentication.Authenticate(tbUsername.Text, tbPassword.Text);

And checking User.Identity.IsAuthenticated but it's returning false until I reach another page. Any suggestions?

like image 723
Fermin Avatar asked May 13 '09 11:05

Fermin


2 Answers

Well, FormsAuthentication.Authenticate() returns a boolean, so you will know whether or not authentication was successful. If it was, you can pass the user's username to Membership.GetUser() and access the returned MembershipUser object to grab whatever data you need and store it in the session.

if (FormsAuthentication.Authenticate(tbUsername.Text, tbUsername.Text)) {
   MembershipUser user = Membership.GetUser(tbUsername.Text);
   // Set session variables here.
   FormsAuthentication.RedirectFromLoginPage(tbUsername.Text, rememberMe.Checked);
}
like image 61
Jonathan Freeland Avatar answered Oct 23 '22 04:10

Jonathan Freeland


Depends on how your doing it but if you are NOT using the ASP Login control and you are doing the auth yourself you need to set the AuthCookie like this...

string userName = UserName.Text;
string password = Password.Text;

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

   MembershipUser user = Membership.GetUser();

   if(user != null){
      // success
   }
   else{
      // failed
   }
}
like image 29
Gabe Avatar answered Oct 23 '22 03:10

Gabe