Using Login control, we can have a ready to use interface. We integrate login controls with ASP.Net membership and form authenticate to automate user authentication for a website.
When a user click on Login button of Login control, Control validates the user name and password by itself by hitting membership API function Membership.ValidateUse(). Then if Membership.ValidateUse() is successful then it calls FormAuthentication.redirectFromLoginPage(). All options on Login user control affect the inputs delivered by the control of these methods i.e. if user clicks the "Remember me next Time" check box control then it passes the value of check box to createPresistentCookie parameter of RedirectFromLoginPage() method and by the result of it FormAuthenticateModule creates or removes a persistent cookie.
In case, we don't want to use membership database and instead of it we want to use our own database then we cannot use this control as it is. We will have to write our own code for handling the authentication and authorization process.
Note - Login controls might not function correctly if the Method of the ASP.NET Web page is changed from POST (the default) to GET.
Sample
Default.aspx
<asp:Login ID="LoginControlName" 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."" onauthenticate="LoginControlName_Authenticate" onloginerror="LoginControlName_LoginError">
Default.aspx.cs
Add following methods
protected void LoginControlName_Authenticate(object sender, AuthenticateEventArgs e)
{
if (YourValidationFunction(LoginControlName.UserName, LoginControlName.Password))
{
//Successfully Logged In
e.Authenticated = true;
//[Your code will go here]
}
else
{
//Login failed
e.Authenticated = false;
}
}
protected void LoginControlName_LoginError(object sender, EventArgs e)
{
if (LoginControlName.PasswordRecoveryUrl != string.Empty)
Response.Redirect(LoginControlName.PasswordRecoveryUrl);
}
private bool YourValidationFunction(string UserName, string Password)
{
bool boolReturnValue = false;
//Validate from database
boolReturnValue = [Return true if exists in database otherwise false]
return boolReturnValue;
}
Happy Coding!!
0 Comment(s)