如何使用&QUOT登录;用户名和QUOT;代替"电子邮件和QUOT;在MVC身份? [英] How to login with "UserName" instead of "Email" in MVC Identity?

查看:231
本文介绍了如何使用&QUOT登录;用户名和QUOT;代替"电子邮件和QUOT;在MVC身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置我的登录用户名使用,而不是电子邮件地址,我能怎样改变?

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

推荐答案

它使用实际的电子邮件地址作为用户名,所以在ASPNetUsers表,你会看到两者的电子邮件地址的用户名和电子邮件领域。

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.

走进了的AccountController,寻找注册方法(POST)。

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

更改此:

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

这样:

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

然后进入Login.cshtml和更改所有相应的电子邮件模型字段的用户名来代替。

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

最后,进入了的AccountController登录方法(POST),并更改为model.Email model.UserName。

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

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

您还必须作出AccountViewModels.cs变化,以引入新的UserName属性。

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

这篇关于如何使用&QUOT登录;用户名和QUOT;代替"电子邮件和QUOT;在MVC身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆