Saturday 29 December 2012

FormsAuthentication.Authenticate Always Returns false


I was trying to create a custom authentication screen without

 using any of the standard ASP.Net security controls (i.e. Login control). 

 So, I created my custom form and was calling the

 System.Web.Security.FormsAuthentication.Authenticate(UserName, Password) method. 

 However, it was always returning false and in fact did not appear

 to even be hitting my SQL database (which contained the security) 

as the failed login attempts, etc. were all unaltered.  Furthermore,

 I changed my connection string to be a bogus reference and the Authenticate

 still simple returned false (I was expecting it to error). 

 I then found out that the

 System.Web.Security.FormsAuthentication.Authenticate(UserName, Password) method is

 for user information stored in the web.config file.  Instead,

 I used the System.Web.Security.Membership.ValidateUser(UserName, Password) method

 which is for the user information stored in the SQL database.


NOTE: When doing this I also then set the authorization cookie by calling:

System.Web.Security.FormsAuthentication.SetAuthCookie(UserName, true);

Here is some sample code:

static public string Login(string UserName, string Password)
{
  if (System.Web.Security.Membership.ValidateUser(UserName, Password))
  {
    System.Web.Security.FormsAuthentication.SetAuthCookie(UserName, true);
    return "1|" + DateTime.Now.ToString();
  }
  else
    return "0|Login failed!";
}



4 comments:

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...