Wednesday 10 April 2013

User Authentication Using Web Services and JSON in ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginJSONt.aspx.cs" Inherits="JSON_JQUERY_AJAX_LoginJSONt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login User</title>
    <script src="../JQuery_Nitish/jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
             $('#btnLog').click(function() {         
             if(($("#txtName").val() != "" ) && ($("#txtpwd").val() != ""))
             {
                    $.ajax({
                        type: "POST",
                        url: "../MyWebService/login.asmx/UserLog",
                        //url: "http://localhost/InsertUsingJSON/Service.asmx/SaveData", -> Not Worked.
                        data: "{'firstName': '" + $('#txtName').val() + "','lastName': '" + $('#txtpwd').val() + "'}",
                        contentType: "application/json; charset=utf-8",
                        success: function(result){
                        //alert(result.d );
                        if(result.d==true){
                        $("#lblMsg").text("Logged Inn.");}
                        else
                        {$("#lblMsg").text("Invalid User.");}
                        //alert(result.d); 
                        },
                        error: function(bad) {
                        //alert("Error");
                        alert(bad.responseText);
                        }
                   });
              }
              else
              {
                    alert ("Must Fill Both Fields.");
              }
                });
             
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
        <asp:Label ID="lblMsg" runat="server" />
            <table align="center" style="border :1px solid green;">
                <tr><td>
                    <asp:TextBox ID="txtName" runat="server" placeholder="User ID" Style="border: 1px solid ThreeDLighShadow;"/>
                    <asp:TextBox ID="txtpwd" runat="server" TextMode="Password" AutoComplete="off" Style="border: 1px solid ThreeDLighShadow;" />
                    <%--<asp:Button ID="btnLog" runat="server" Text="GO" ToolTip="Login" Style="background-color: Olive;
                        border: 1px solid green;" />--%>
                        <input type="button" id="btnLog" value="Get Inn" Style="background-color: Lime;
                        border: 1px solid ThreeDLighShadow;" /><!--   HTML Button Works Properly -->
               </td> </tr>
            </table>
        </center>
    </div>
    </form>
</body>
</html>



**  Web Service **

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]
public class login : System.Web.Services.WebService
{

    public login() { }



    [WebMethod]
    public bool UserLog(string firstName, string lastName)
    {
        bool result = false;
        DataTable dt = new DataTable();
        using (SqlConnection connection = new SqlConnection("Server=iyc1server; database=test_base; uid=sa; pwd=saa;"))
        {
            string qry = "Select Top 1 FirstName, LastName From PersonName Where  FirstName='" + firstName + "' and LastName='" + lastName + "'";
            connection.Open();
            using (SqlDataAdapter da = new SqlDataAdapter(qry,connection))
            {
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            return result;
        }
    }

}