无法使用.net core 2.0中的已创建用户登录

时间:2018-04-29 17:32:45

标签: c# asp.net-core

我正在使用.net core 2.0全新的项目。在使用Identity构建我的角色时,我也在创建一个用户。要创建用户,我有以下代码。

var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
        string[] roleNames = { "SuperAdmin", "Admin", "Support", "User" };

        IdentityResult roleResult;

        foreach (var roleName in roleNames)
        {
            var roleExist = await roleManager.RoleExistsAsync(roleName);
            if (!roleExist)
            {
                //create the roles and seed them to the database: Question 1
                roleResult = await roleManager.CreateAsync(new IdentityRole(roleName));
            }
        }

        // administrator
        var user = new ApplicationUser
        {
            UserName = "Administrator",
            Email = "something@something.com",
            EmailConfirmed = true
        };
        var i = await userManager.FindByEmailAsync(user.Email);
        if (i == null)
        {
            var adminUser = await userManager.CreateAsync(user, "Testing1!");
            if (adminUser.Succeeded)
            {
                await userManager.AddToRoleAsync(user, "SuperAdmin");
            }
        }

用户创建,但当我尝试使用电子邮件和密码登录时,它表示无效登录。

3 个答案:

答案 0 :(得分:0)

对于PasswordSignInAsync方法,您正在使用...

  

等待_signInManager.PasswordSignInAsync(model.Email,model.Password,   model.RememberMe,lockoutOnFailure:false);

它需要用户名而不是电子邮件。 SignInManager上没有使用字符串email作为参数来登录密码的方法。

    public virtual async Task<SignInResult> PasswordSignInAsync(string userName, string password,
        bool isPersistent, bool lockoutOnFailure)

请参阅: https://github.com/aspnet/Identity/blob/dev/src/Identity/SignInManager.cs#L297

如果您想使用电子邮件地址而不是用户名,请查看以下内容:

public virtual async Task<SignInResult> CheckPasswordSignInAsync(TUser user, string password, bool lockoutOnFailure)

或者:

public virtual async Task<SignInResult> PasswordSignInAsync(TUser user, string password,
        bool isPersistent, bool lockoutOnFailure)

您的评论:

  

默认的Identity安装使用用户名,它设置为   有人注册时的电子邮件然后价值与我的原因相同   需要在创建时将用户名更改为电子邮件。谢谢   虽然这个回应。这将帮助我不必经常做   如果有人想要更改他们的登录信息,我确定我会更新两个字段   电子邮件。

默认值可能是这样;但是,它不反映实际代码以及如何解析登录。源清楚地使用userName而不是emailAddress来查找具有您正在使用的登录操作的用户。

   public virtual async Task<SignInResult> PasswordSignInAsync(string userName, string password,
        bool isPersistent, bool lockoutOnFailure)
    {
        var user = await UserManager.FindByNameAsync(userName);
        if (user == null)
        {
            return SignInResult.Failed;
        }

        return await PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure);
    }

答案 1 :(得分:0)

替代解决方案

此方法使用UserManager<T>通过电子邮件或userName查找用户,然后使用SignInManager<T>尝试使用PasswordSignInAsync重载方法登录用户:

public virtual async Task<SignInResult> PasswordSignInAsync(TUser user, string password,
        bool isPersistent, bool lockoutOnFailure)

在此示例T is an ApplicationUser : IdentityUser

    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;

    ....

   /// <summary>
    /// Handle postback from username/password login
    /// </summary>
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginInputModel model, string button)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                return await ReturnLoginError(model);
            }

            ApplicationUser user = await _userManager.FindByEmailAsync(model.Email) 
                                   ?? await _userManager.FindByNameAsync(model.Username);

            SignInResult signInResult = await _signInManager.PasswordSignInAsync(user, model.Password, isPersistent: true,
                lockoutOnFailure: true);

             ...
      }

这将允许您使用电子邮件地址或用户名来执行登录。

答案 2 :(得分:-2)

要解决此问题,我必须将用户名和电子邮件设置为与我希望用户通过电子邮件登录的值相同。

PasswordSignInAsync想要用户名不是电子邮件,所以我不得不使用它。

// administrator
    var user = new ApplicationUser
    {
        UserName = "something@something.com",
        Email = "something@something.com",
        EmailConfirmed = true
    };
相关问题