How to do form authentication using sql server as database in asp.net ?



Hi

Since you are knowing that form authentication is more secure as compare to other authentication in asp.net. You can read more on “Authentication and authorization” from this excellent artical.
Authentication and authorization

We can do form authentication using sql server as database in asp.net as given bellow steps

Step1: Take a “Login” Control from asp.net Toolbox and kept on “Login.aspx” page.

Step2: Create “Home.aspx” page which will display after proper authentication.

Step3: Now click on “Login1_Authenticate” event of “Login” control of Login.aspx page and write the code like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        using (SqlConnection con = new SqlConnection(" Here will be your connection string"))
        {
            using (SqlCommand cmd = new SqlCommand("Select UserName,Password from tblLogin where UserName=@UserName and Password=@Password", con))
            {
                cmd.Parameters.AddWithValue("@UserName", Login1.UserName);
                cmd.Parameters.AddWithValue("@Password", Login1.Password);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    //This code is generating the Cookies 
                    FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);

                }
            }
        }
    }
}

Step4: Now go to webconfig file and make the configuration like this

 <authentication mode="Forms">
      <forms defaultUrl="Home.aspx" loginUrl="~/Login.aspx"
      slidingExpiration="true" timeout="20">
      </forms>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

If authentication will be sucessful then it will display “Home.aspx”.

Advertisements

5 Responses to “How to do form authentication using sql server as database in asp.net ?”

  1. Clyde Says:

    I want to express appreciation to you for rescuing
    me from such a situation. Because of searching
    through the internet and coming across things which were not helpful,
    I figured my life was done. Living without the strategies
    to the problems you have fixed through this short post is a serious case, as well as those that could have in a wrong way damaged my career if I hadn’t noticed your blog. Your understanding and kindness in touching every item was important. I am not sure what I would’ve done if I had not encountered such a solution like this.
    I am able to at this moment look ahead to my future.
    Thanks very much for this impressive and effective
    guide. I will not be reluctant to endorse your
    web site to anybody who ought to have direction about this problem.

    • Chandra Dev Says:

      Hi
      You are welcome and thanks for sending your kind reply to motivate me to write good post like this.

  2. samyek10 Says:

    I have made a webpage with log-in module. After an user log out, if the any logged webpage’s url is entered manually the authenticated page till display. Can you show how to prevent this?

    • Chandra Dev Says:

      Hi
      You can do like this
      http://www.mindfiresolutions.com/How-to-Prevent-visiting-pages-from-History-after-Logout-118.php
      Please let me know if it doesnot work.

      • Chandra Dev Says:

        Hi

        You can also do like this

        Add these lines in the html meta section

        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
        <META HTTP-EQUIV="EXPIRES" CONTENT="0"> 
        
        

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: