I'm trying to figure out how to implement an AJAX login for a ASP.NET 2.0 site with Jquery. I already have a implemented other simple Jquery AJAX application on the site, but I'm unsure how to convert over the standard login control to POST via AJAX. Should I expose the login.aspx page methods? Any help would be great.
Here are some ideas on how this can be implemented. This is not full code, but it should be enough to get you started on the right track.
You need to create your own login form fields for username/password.
Create an ASMX or WCF WebService for authentication with a method similar to this:
[WebMethod]
public string AuthenticateUser(string username, string password)
{
string result = "Invalid Username or Password";
if(Membership.ValidateUser(userName, password))
{
FormsAuthentication.SetAuthCookie(u.UserName, false);
result = "successful";
}
return result;
}
Then from your login button's click event you can use jQuery ajax to post the username/password to the webservice:
$.ajax({
type: "POST",
url: "WebService.asmx/AuthenticateUser",
data: "{username:"+$('#txtUsername').val()+",password:"+$('#txtPassword').val()+"}",
success: function(result) {
alert(result);
//if(result=='successful')
// redirectUser to the home page
}
});
With this solution there is a big security problem that username and password will be send in plain-text format. so you should use SSL or hash these data in some way. take a look here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With