Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 2.0 JQuery AJAX Login

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.

like image 819
GreenEggs Avatar asked May 18 '09 16:05

GreenEggs


2 Answers

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
  }
});
like image 72
Jose Basilio Avatar answered Nov 18 '22 12:11

Jose Basilio


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

like image 25
Mahdi Avatar answered Nov 18 '22 11:11

Mahdi