Login page in asp.net using c# forms authentication complete guide

hi there in this post we tell you about Login page in asp.net using c# forms authentication complete guide so now you can create login page in asp.net using # and database.

 Login page in asp.net using c# and sql database forms authentication

In this post we will tell you to create login page in asp.net using c# and ms sql server in some step, only you need to follow all step to create login page in asp.net.

Start Visual Studio

Now in this tutorial we will use of visual studio 2010 and sql server 2008 you can use any version of sql server and must use visual studio 2010 later version.
Here we will need to open visual studio create first website for C# code and select empty Website.

Create Databse in sql Server

CREATE TABLE [dbo].[tbl_login](
[user_name] [nvarchar](50) NULL,
[password_] [nvarchar](50) NULL
) ON [PRIMARY]
GO

Create Connection String in web.config

<?xml version=”1.0″?>

<!–
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  –>

<configuration>
  <connectionStrings>
    <add name=”connect” connectionString=”Data Source=SARTHAK-PCSQLEXPRESS;Initial Catalog=login_page;Integrated Security=True” providerName=”System.Data.SqlClient”/>
  </connectionStrings>

    <system.web>
        <compilation debug=”false” targetFramework=”4.0″ />
    </system.web>

</configuration>

Code of  Default.aspx

You need only put this control to your design page.

     <asp:TextBox ID=”txtusername” class=”input100″ placeholder=”Type your username”  runat=”server”></asp:TextBox>
                          <asp:TextBox ID=”txtpassword” TextMode=”Password” class=”input100″ placeholder=”Type your password”  runat=”server”></asp:TextBox>

<asp:Button ID=”btnlogin” class=”login100-form-btn” runat=”server” Text=”Login” onclick=”btnlogin_Click” />

Create Connection Default.aspx.cs

 SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings[“connect”].ToString());

Open Connection Default.aspx.cs

con.Open();

Sql Query in Default.aspx.cs

 String query=”select count(*) from tbl_login where user_name='”+txtusername.Text+”‘ and password_='”+txtpassword.Text+”‘”;

Sql Command in Default.aspx.cs

SqlCommand cmd=new SqlCommand(query,con);

Use Excutenoquery  in Default.aspx.cs

 String output=cmd.ExecuteScalar().ToString();
        if(output==”1″)
        {
            Session[“User”] = txtusername.Text;
            Response.Redirect(“~/Welcome.aspx”);
        }
        else
        {
            Response.Write(“Your User Name and Password is wrong !”);
        }

Final Code of Default.aspx.cs

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.Configuration; public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {         if (Session[“User”] != null)         {             Response.Redirect(“~/Welcome.aspx”);         }     }
    protected void btnlogin_Click(object sender, EventArgs e)     {         SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings[“connect”].ToString());         con.Open();         String query=”select count(*) from tbl_login where user_name='”+txtusername.Text+”‘ and password_='”+txtpassword.Text+”‘”;         SqlCommand cmd=new SqlCommand(query,con);         String output=cmd.ExecuteScalar().ToString();         if(output==”1″)         {             Session[“User”] = txtusername.Text;             Response.Redirect(“~/Welcome.aspx”);         }         else         {             Response.Write(“Your User Name and Password is wrong !”);         }     } }

Code of Welcome.aspx

 <asp:Button ID=”btnlogout” runat=”server” Text=”Logout”
            onclick=”btnlogout_Click” />

Code of Welcome.aspx.cs

   protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[“User”] != null)
        {
            Response.Write( “Welcome ” +Session[“User”]);
        }
    }
    protected void btnlogout_Click(object sender, EventArgs e)
    {
        Session.Remove(“User”);
        Response.Redirect(“~/Default.aspx”);
    }

My Youtube Video for Login page in asp.net using C#


Please Like my Video and Subscribe my Channel.

If you have any problem in web development then you can ask question.
Thanks a lot.

Download Source Code

Donate me

Be the first to comment

Leave a Reply

Your email address will not be published.


*