The ASP.NET login controls provide a robust login solution for ASP.NET Web applications without requiring programming. By default, login controls integrate with ASP.NET membership and forms authentication to help automate user authentication for a Web site. It provides you with a ready-to-use user interface that queries the user name and password from the user and offers a Log In button for login. It validates user credentials against the membership API and encapsulates the basic forms authentication functionality like redirecting back to the original requested page in a restricted area of your application after the successful login.
The Login control displays a user interface for user authentication. The Login control contains text boxes for the user name and password and a check box that allows users to indicate whether they want the server to store their identity using ASP.NET membership and automatically be authenticated the next time they visit the site.
The Login control has properties for customized display, customized messages, and links to other pages where users can change their password or recover a forgotten password. The Login control can be used as a standalone control on a main or home page, or you can use it on a dedicated login page. If you use the Login control with ASP.NET membership, you do not need to write code to perform authentication. However, if you want to create your authentication logic, you can handle the Login control's Authenticate event and add a custom authentication code.
Note. Login controls might not function correctly if the Method of the ASP.NET Web page is changed from POST (the default) to GET.
- Start Microsoft Visual Studio 2008
- Create a new ASP.NET WebSite, Like this.
- Drag and drop the login control on pa age from ToolBox.
Figure 1.
Whenever the ser hits the login button, the control automatically validates the user name and password using the membership API function Membership.ValidateUse() and then ccallFormAuthentication.redirectFromLoginPage() if the validation was successful. All options on the UI of the LoginControl affect the input delivered by the control to these methods. For Example, if you click the "Remember me next time" check box, it passes the value true to the createPresistentCookie parameter of the RedirectFromLoginPage() method. Therefore, the FormAuthenticateModule creates a persistent cookie.
There are three Login Tasks by default.
You can change the styles of LoginControl using css too, Like this.
.LoginControl
{
background-color:#F7F7DE;
border-color:#CCCC99;
border-style:solid;
border-width:1px;
font-family:Verdana;
font-size:10px;
}
And now apply CSS to control.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Control</title>
<link href="StyleSheet.css" type="text/css" rel="Stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" CssClass="LoginControl">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
</div>
</form>
</body>
</html>
If you running the page and if the CSS file is placed in a directory where anonymous access is denied, add the following configuration for the CSS file to your web.config file.
<location path="StyleSheet.css">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
You can add several hyperlinks to your Login control, such as a hyperlink to a help text page, or a hyperlink to to a registration page.
<asp:Login ID="Login1" runat="server" CssClass="LoginControl"
CreateUserText="Register"
CreateUserUrl="~/Register.aspx"
HelpPageText="Additional Help"
HelpPageUrl="~/Help.aspx"
InstructionText="Please enter your user name and password for login.">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
Looks like this
Here is.CS Code
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
ViewState["LoginErrors"] = 0;
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (YourValidationFunction(Login1.UserName, Login1.Password))
{
// e.Authenticated = true;
Login1.Visible = false;
MessageLabel.Text = "Successfully Logged In";
}
else
{
e.Authenticated = false;
}
}
protected void Login1_LoginError(object sender, EventArgs e)
{
if (ViewState["LoginErrors"] == null)
ViewState["LoginErrors"] = 0;
int ErrorCount = (int)ViewState["LoginErrors"] + 1;
ViewState["LoginErrors"] = ErrorCount;
if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))
Response.Redirect(Login1.PasswordRecoveryUrl);
}
private bool YourValidationFunction(string UserName, string Password)
{
bool boolReturnValue = false;
string strConnection = "server=.;database=Vendor;uid=sa;pwd=wintellect;";
SqlConnection sqlConnection = new SqlConnection(strConnection);
String SQLQuery = "SELECT UserName, Password FROM Login";
SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
SqlDataReader Dr;
sqlConnection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))
{
boolReturnValue = true;
}
}
Dr.Close();
return boolReturnValue;
}
}
If you insert the wrong username and password then the message will show like this.
If you insert the right username, and password then redirect your page wherever you want or you can show ma message in ErrorLabel, Like this.
I am attaching my database with the application in the pp_Data folder, if u want to use my database then attach my MDF file.
Any questions or queries ask me any time.