How to use Login Control in ASP.Net - Basics
|
|
|
|
|
Downloads
If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link
and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to
become a Winista member and download the control for free.
This article will show you how to use Login control in ASP.Net applications. The article will not dig deep into advanced usage of
this control or customization of this contro. I will discuss simple usage. I will be talking about advanced settings and usage
in other articles.
Login control can use either MembershipProvider specified in web.config file, or can explicitly assign Membership provide or
you can do custom authenication. To start with basics lets assume that you already have authentication methods implemented using
your existing database and you want to leverage that infrastructure with Login control. So what you need to do is, add an handler
for OnAuthenticate event handler for your control and authenticate user via that method.
Redirecting After Login
If authentication fails user stays on the same login page. On successful login the user gets redirected to appropriate
page. There are couple of ways of specifying the page where user gets sent after successful authentication.
- A query string parameter ReturnUrl with a value of URL for redirection is one way. After authentication the
implemnetation of Login control will look at this query string parameter and redirect the user to that URL.
- You can set
DestinationPageurl property of the control and set the URL of target page in there.
- You can handle
OnLoggedin event of the control and manually call Response.Redirect to targer page.
Following code snippets show what the attributes on page look like and how OnAuthenticate event handler sets
Authenticated value in the event argument.
<asp:login id="ctlLogin" runat="server" onauthenticate="OnAuthenticate"
onloggedin="OnLoggedIn" onloggingin="OnLoggingIn" destinationpageurl="~">
</asp:login>
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
e.Authenticated = false;
if (String.Compare(ctlLogin.UserName, "foo", true) == 0 &&
String.Compare(ctlLogin.Password, "bar", false) == 0)
{
e.Authenticated = true;
}
}
protected void OnLoggedIn(object sender, EventArgs e)
{
Response.Redirect("~");
}
|