This article will be explain how to create custom login page from database table without using login control in Asp.Net. In this article we are creating login page as well as Registration page.
Step 1
In first step open your webconfig file and write following code in configuration.
In step 2 open Default.aspx page and add following control.
In Defalut.aspx.cs page add following code.
Step 1
In first step open your webconfig file and write following code in configuration.
<connectionStrings>
<add name="ConnectionString" connectionString="Your connection string will be here"/>
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms loginUrl="Default.aspx"
/>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<location path="Allow">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Step 2In step 2 open Default.aspx page and add following control.
<form id="form1" runat="server">
<div>
<p>
<strong>Email:</strong>
<br />
<asp:TextBox ID="txtUName" runat="server" Columns="50"></asp:TextBox>
</p>
<p>
<strong>Password:</strong>
<br />
<asp:TextBox ID="txtPass" runat="server" Columns="50" TextMode="Password"></asp:TextBox>
</p>
<p>
<asp:Button ID="btnLogin" runat="server" Text="Logon" OnClick="bnLogon_Click"/>
<br />
<asp:Label ID="lblMessage" ForeColor="red" runat="server" />
<asp:LinkButton ID="BtnReg" runat="server"OnClick="LinkButton1_Click">Registration</asp:LinkButton>
</p>
</div>
</form>
Step 3In Defalut.aspx.cs page add following code.
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Security.Cryptography;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Request.Params["logout"]))
{
FormsAuthentication.SignOut();
Response.Redirect("./");
}
}
protected void bnLogon_Click(object sender, EventArgs e)
{
if (ValidUser(txtUName.Text, txtPass.Text))
{
Response.Redirect("welcome.aspx");
}
else
{
lblMessage.Text = "Incorrect";
}
}
bool ValidUser(string user, string pass)
{
string connStr =ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = "select * from LoginTable where email = @email and password = @password";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@email", user);
cmd.Parameters.AddWithValue("@password", pass);
return cmd.ExecuteScalar() is string;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Allow/Registration.aspx");
}
}
No comments:
Post a Comment