Home > ASP.Net > Allow User to Login Using UserName or Email

Allow User to Login Using UserName or Email


Hi all,

Try this example to allow user to Login using UserName Or Email address using Membership Provider

In Login Page :


<asp:Login runat="server" ID="Login1" DisplayRememberMe="false"
 DestinationPageUrl="/Users/Default.aspx" onloggingin="Login1_LoggingIn" >
</asp:Login>

In Code behind :


protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
 {
 if (Login1.UserName.Contains("@") && Membership.GetUser(Login1.UserName) == null)
 {
 string Username = Membership.GetUserNameByEmail(Login1.UserName);
 if (Username == null)
 return;

 if (Membership.ValidateUser(Username, Login1.Password) == true)
 {
 FormsAuthentication.SetAuthCookie(Username, false);
 FormsAuthentication.RedirectFromLoginPage(Username, false);
 e.Cancel = true;
 }
 }
 }

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: