Wednesday, 12 February 2014

Here I am gonna explain about how to checking the user authentication against user sql server database.

Step1:

Creating the very simple stored procedure for checking the user is authenticated one or not using below sql query.

create  procedure [dbo].[SP_Authentication]

@username  varchar(250),
@password  varchar(250)


as
begin

declare @count int
if  exists (select * from  userregister where u_name=@username  and u_password=@password )
begin
set @count = 1

end
else
begin
set @count=-1
end
select @count as returnvalue
end


Step 2:

Write a below c# code for checking the user is authenticated one or not.

  using (SqlConnection con = new SqlConnection(connectionstring))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SP_Authentication", con);
            cmd.CommandType = CommandType.StoredProcedure;
            string password=FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1");
            cmd.Parameters.Add("@username",TextBox1.Text);
            cmd.Parameters.Add("@password", password);

            int flag = (int)cmd.ExecuteScalar();

            if (flag == 1)
            {
                FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
            }

            else
            {   //not authenticated user
                Label1.Text = "Invalid User";
            }
            con.Close();

        }

 Output


                              
                                                            Thank you guys........!


No comments:

Post a Comment