如何在MVC中使用“用户名”而不是“Email”登录?

时间:2022-08-10 19:20:41

I need to set my login to use username instead of email address, how can I change it?

我需要设置我的登录以使用用户名而不是电子邮件地址,我如何更改它?

1 个解决方案

#1


15  

It's actually using the e-mail address as the username, so in the ASPNetUsers table, you'll see both the username and email fields with the email address.

它实际上使用电子邮件地址作为用户名,所以在ASPNetUsers表中,您将看到带有电子邮件地址的用户名和电子邮件字段。

Go into the AccountController, look for Register method (POST).

进入AccountController,查找注册方法(POST)。

Change this:

改变:

var user = new ApplicationUser { UserName = model.Email, Email = model.Email};

to this:

:

var user = new ApplicationUser
            {
                UserName = model.UserName,
                Email = model.Email
            };

Then go into the Login.cshtml and change all corresponding e-mail model fields to username instead.

然后进入登录。cshtml并将所有相应的电子邮件模型字段改为用户名。

Finally, go into the Login method (POST) in the AccountController and change model.Email to model.UserName.

最后,进入AccountController中的登录方法(POST)并更改模型。电子邮件model.UserName。

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, 
             model.RememberMe, shouldLockout: false);

You also have to make changes in AccountViewModels.cs in order to introduce your new UserName property.

您还必须在accountviewmodel中进行更改。cs来介绍你的新用户名属性。

#1


15  

It's actually using the e-mail address as the username, so in the ASPNetUsers table, you'll see both the username and email fields with the email address.

它实际上使用电子邮件地址作为用户名,所以在ASPNetUsers表中,您将看到带有电子邮件地址的用户名和电子邮件字段。

Go into the AccountController, look for Register method (POST).

进入AccountController,查找注册方法(POST)。

Change this:

改变:

var user = new ApplicationUser { UserName = model.Email, Email = model.Email};

to this:

:

var user = new ApplicationUser
            {
                UserName = model.UserName,
                Email = model.Email
            };

Then go into the Login.cshtml and change all corresponding e-mail model fields to username instead.

然后进入登录。cshtml并将所有相应的电子邮件模型字段改为用户名。

Finally, go into the Login method (POST) in the AccountController and change model.Email to model.UserName.

最后,进入AccountController中的登录方法(POST)并更改模型。电子邮件model.UserName。

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, 
             model.RememberMe, shouldLockout: false);

You also have to make changes in AccountViewModels.cs in order to introduce your new UserName property.

您还必须在accountviewmodel中进行更改。cs来介绍你的新用户名属性。