c# - ASP.NET Identity 2 邮箱和用户名分离并使用邮箱登录

标签 c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity-2

默认情况下,ASP.NET Identity 将 UserName 和 Email 字段作为用户输入的电子邮件。我已经更改了它,以便我的系统为用户采用单独的用户名和电子邮件。

我的问题是现在我无法登录系统,因为身份会对用户名和电子邮件进行检查。

我想知道是否有办法让这两个字段保持独立和唯一,同时能够只检查电子邮件字段以登录系统?

我注意到 public virtual Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout);检查用户名,但将其更改为电子邮件没有任何作用,我仍然无法登录。

我还查看了这些其他问题:

ASP.Net UserName to Email

Allowing both email and username for authentication

寻找可能的解决方案,但没有发现任何真正能让我做我想做的事。

为了拥有单独的用户名和电子邮件,同时仍然能够使用电子邮件字段登录,还有什么我可以尝试的吗?

最佳答案

因此,看起来您只能使用用户名进行身份验证,但没有理由无法从电子邮件地址中找到用户名:

var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null)
{

    return //some error or throw
}
var result = await 
    SignInManager.PasswordSignInAsync(user.UserName, 
                                      model.Password, 
                                      model.RememberMe, 
                                      shouldLockout: false);

关于c# - ASP.NET Identity 2 邮箱和用户名分离并使用邮箱登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146539/

相关文章:

c# - 如何在 Controller 中获取数据库上下文

c# - 身份的扩展

c# - Blazor - 将参数传递给父级

c# - 为什么无法在 asp.net 中获取正确的下拉列表值

asp.net - 将参数传递到 ASP .NET 页面

c# - 定位代码在 li 内生成的数据列表项

asp.net-mvc - ASP.NET MVC 4 货币字段

c# - 在扩展方法中实例化空对象

c# - JQuery:如何使用 live 和 post 函数

c# - 无法设置类型的属性,因为集合已设置为 EntityCollection