Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
Dear,

I created OData Services with Custom Authentication, How to pass the UserName & Password in Ajax. Note that without having security below code working fine.

Please help on this

Below is my Code
JavaScript
var _url = "http://localhost:49497/DataService.svc/Mst_User?$filter=Us_Name eq 'Basit' and Us_Password eq 's'&$format=json";

$.ajax({
                type: "GET",
                url: _url,
                timeout: 10000,
                async: true,
                dataType: "json",
                contentType: "application/jsonp; charset=utf-8",
                success: function (data)
                    debugger;
                    //
                    if (data.d.length > 0 && data.d[0].Us_Admin == 1) {

                        var Adminrole = data.d[0].Us_AdminModule
                        var UsNo = data.d[0].Us_No


                    } else {
                        Alter(‘Login Failed’)
                    }
                },
                error: function (xhr, status, error) { // flow always comes in error callback even url returns true and this is the issue.
                        Alter(‘Login Failed’)
                }
            });
        }
    };


Thanks
Basit
Posted
Updated 16-Jul-15 12:03pm
v2
Comments
CodingLover 16-Jul-15 23:13pm    
So do you want to encrypt the data and pass?
yeleswarapu ramakrishna 17-Jul-15 3:46am    
why don't you use post instead of get for type.

JavaScript
var _url = "http://localhost:49497/DataService.svc/Mst_User?$filter=Us_Name eq 'Basit' and Us_Password eq 's'&$format=json";

  $.ajax({
                  type: "POST",
                  data: "{ strFUserName: '" + VariableUsername + "', strFPassword: '" + Variablepassword + "'}",
                  url: _url,
                  timeout: 10000,
                  async: true,
                  dataType: "json",
                  contentType: "application/json; charset=utf-8",
                  success: function (data)
                      debugger;
                      //
                      if (data.d.length > 0 && data.d[0].Us_Admin == 1) {

                          var Adminrole = data.d[0].Us_AdminModule
                          var UsNo = data.d[0].Us_No


                      } else {
                          Alter(‘Login Failed’)
                      }
                  },
                  error: function (xhr, status, error) { // flow always comes in error callback even url returns true and this is the issue.
                          Alter(‘Login Failed’)
                  }
              });
          }
      };
 
Share this answer
 
I suggest you to...

  • Store hashed password in database instead of plain text.
  • Make that service HTTPS enabled so that connection will be secure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900