Access control in ASP.NET, email login, but email is not a username (how)?

Using ASP.NET forms authentication, how do you accept an email address and password, use an email address to search for a username, and then write them down by searching for a username and password?

We have this card system with employee numbers, but no one remembers their numbers. Using email addresses for logging in will be obvious, but the rest of the application depends on the username being the employee number.

Thanks. Links to code / examples would be great.

Our current C # solution ends with an empty reference exception:

Response.Write(Membership.GetUser().UserName);

but both of these functions are wonderful:

string userName = Membership.GetUserNameByEmail(emailAddress);
bool successfulLogin = Membership.ValidateUser(userName, password);

+3
4

. . , FormsAuthentication.RedirectFromLoginPage.

, , , .

. docs.

+3

, 'Authenticate', . FormsAuthentication.SetAuthCookie() .

, :

private void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
    bool successfulLogin = false;
    string userName = Membership.GetUserNameByEmail(Login1.UserName); //the email address
    successfulLogin = Membership.ValidateUser(userName, Login1.Password);

    if(successfulLogin)
      FormsAuthentication.SetAuthCookie(userName, true);

    e.Authenticated = successfulLogin;
}
+10

. :

Response.Write(HttpContext.Current.User.Identity.Name);

But this tells me that the current user is null:

Response.Write(Membership.GetUser().UserName);
+1
source

This is my solution and everything is fine for me LoginUser- this is the name of the input control.

protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
{
    bool successfulLogin = false;
    string userName = Membership.GetUserNameByEmail(LoginUser.UserName); //the email address
    LoginUser.UserName = userName; //+++ Set username recovered by email. 
    successfulLogin = Membership.ValidateUser(userName, LoginUser.Password);

    if (successfulLogin)
        FormsAuthentication.SetAuthCookie(userName, true);

    e.Authenticated = successfulLogin;
}
0
source

Source: https://habr.com/ru/post/1718238/


All Articles