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........!


Simple User Registration


Here I am gonna explain about how to registered user details into sql server database.

User Registration Steps:

Step 1:
Create the Table structure based on the below screen shot (table name: userregister)

   CREATE TABLE [dbo].[userregister]
   (
     [u_id] [int] IDENTITY(1,1) NOT NULL,
     [u_name] [varchar](250) NULL,
     [u_password] [varchar](250) NULL,
     [u_email] [varchar](250) NULL
     )

Step 2:

Create the very simple stored procedure for storing the user details and check whether the username is already exists or not in the database using SP_UserRegister stored Procedure

Create procedure [dbo].[SP_UserRegister]

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

as
begin

declare @count int
if not exists (select * from  userregister where u_name=@username)
begin
set @count = 1
insert into userregister values(@username,@password,@email)
end
else
begin
set @count=-1
end
select @count as returnvalue
end


Step 3:

Write a below C# code for supply the user details (username,password,etc..) parameter to stored procedure the procedure will take care of storing the user details to the Database table.

using (SqlConnection con = new SqlConnection(connectionstring ))
        {
            con.Open();
            SqlCommand cmd=new SqlCommand ("SP_UserRegister",con);
            cmd.CommandType = CommandType.StoredProcedure;

string pass= FormsAuthentication.HashPasswordForStoringInConfigFile(Textbox2.Text, "SHA1");

            cmd.Parameters.Add("@username", Textbox1.Text);
            cmd.Parameters.Add("@password", pass); ;
            cmd.Parameters.Add("@email", Textbox3.Text);

            int returncode = (int)cmd.ExecuteScalar();
                     
            con.Close();

            if (returncode == 1)
            {
                //user already not exist exist

                Label1.Text = "Register Sucessfully!";
            }
            else
            {
                // user already exist
                Label1.Text = " USER Already Exist ";
            }


 Output:



Thank you Guys....