Visual Studio 2010 and MS SQL 2008- Authentication System using asp.net ( JQuery, Json and Web servi
In this article, we will be looking at the login page of the application. An authentication system allows certain online material to be accessible only to a select few. This tutorial illustrates the basic construction of an authentication system using asp.net 4.0 in Visual Studio 2010
In this Tutorial, I'll be using JQuery to make AJAX Calls to the server. I'll also explain how to call server side methods or functions directly using JQuery in ASP.Net 4.0 Csharp.Overview
1. Create Project in Visual Studio
2. Solution Explorer
3. Database
1. Create Table
2. Insert statement
3. Create Store Procedure
4. Web.Config
5. Server Side
6. Client Side
First Create a website/Project in Visual Studio 2010.
Next Create a table with Name LoginUser.sql.
User name should be primary key and id should be auto Increment. now question come here why should username be unique because to prevent duplicate user.
now create a store procedure as given in my site.
I have created a method which accepts the user name and password as string and returns the status. If user id exists and if not then 0 returned by the stored procedure based on the availability of user login.
Create a Method as given below
[System.Web.Services.WebMethod]
public static string CheckUserName(string userName,string passWord)
{
string returnValue = string.Empty;
try
{
string consString = ConfigurationManager.ConnectionStrings["testingConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(consString);
SqlCommand cmd = new SqlCommand("SPX_CHECKUSERLOGIN", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", userName.Trim());
cmd.Parameters.AddWithValue("@password", passWord.Trim());
conn.Open();
returnValue = cmd.ExecuteScalar().ToString();
conn.Close();
}
catch(Exception ee)
{
returnValue = "error"+ee.Message;
}
return returnValue;
}
Client Side:
In head tag write Jquery function to call method from server side as given below
function CheckUserLogin() {
$.ajax({
type: "POST",
url: "default.aspx/CheckLogin",
data: '{userName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '",passWord: "' + $("#<%=txtPassWord.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response);
}
});
have you seen in this function data is send user name and password to server side method. I am simply calling the CheckLogin Server side function in Default.aspx page and passing the TextBox values as parameter. Secondly I have defined the success method OnSuccess that will be called handle the response returned by the Server.
Note:Method should be static.
Reference: http://www.aspxtutorial.com/post/2010/05/04/Visual-Studio-2010-and-MS-SQL-2008-Authentication-System-using-aspnet-%28-JQuery-Json-and-Web-services%29.aspx