在ASP.NET Core 5中创建身份验证用户时遇到问题 [英] Problems Creating Auth User in ASP.NET Core 5

查看:56
本文介绍了在ASP.NET Core 5中创建身份验证用户时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自己构建的VS项目,基本上就是一个启用了本地Auth的ASP.NET Core 5网站.我可以注册本地用户,然后再登录就可以了.但是,当我尝试通过下面的代码以编程方式创建用户时,我知道该用户已创建,因为我可以在SQL中对其进行查看,但是当我尝试登录时,会出现不允许登录访问"的提示.错误.

I have a VS Project I built, basically just and ASP.NET Core 5 Web Site with local Auth enabled. I can register local users, then login to them no problem. However, when I try to create a user programmatically, with the code below,I know the user is created because I can view it in SQL, however when I try and login, I get a "login access no allowed" error.

  1. 我是否以某种方式无法正确创建用户?

  1. Am I somehow not creating the user correctly?

我该如何调试它以查看未验证的内容?

How can I debug this to see what is not validating?

public static async Task SeedDefaultUserAsync(UserManager<ApplicationUser> userManager)
{
    string testUser = "user999";
    var user = await userManager.FindByNameAsync(testUser);

    if (user == null)
    {
        user = new ApplicationUser()
        {
            Email = testUser + "@gmail.com",
            UserName = testUser,
            EmailConfirmed = true,
            LockoutEnabled = false,
        };

        await userManager.CreateAsync(user, "Abc123@");
        Console.WriteLine("user: " + testUser + " available with password Abc123@.");
    }

    if (user == null)
    {
        throw new Exception("The password is probably not strong enough!");
    }
}

我什至尝试给自己发送一个密码重设链接,重设密码,但仍然出现登录错误.

I've even tried sending myself a password reset link, reset the password and I still get the login error.

推荐答案

据我所知,当您登录asp.net核心身份时,它将调用CanSignInAsync方法来检查用户是否可以登录.

As far as I know, when you login in asp.net core identity it will called the CanSignInAsync method to check the user could signin or not.

如果用户未通过,它将抛出不允许登录访问".错误.

If user doesn't pass , it will throw the "login access no allowed" error.

更多详细信息,您可以参考以下源代码:

More details, you could refer to below source codes:

    protected virtual async Task<SignInResult> PreSignInCheck(TUser user)
    {
        if (!await CanSignInAsync(user))
        {
            return SignInResult.NotAllowed;
        }
        if (await IsLockedOut(user))
        {
            return await LockedOut(user);
        }
        return null;
    }



   public virtual async Task<bool> CanSignInAsync(TUser user)
    {
        if (Options.SignIn.RequireConfirmedEmail && !(await UserManager.IsEmailConfirmedAsync(user)))
        {
            Logger.LogWarning(0, "User cannot sign in without a confirmed email.");
            return false;
        }
        if (Options.SignIn.RequireConfirmedPhoneNumber && !(await UserManager.IsPhoneNumberConfirmedAsync(user)))
        {
            Logger.LogWarning(1, "User cannot sign in without a confirmed phone number.");
            return false;
        }
        if (Options.SignIn.RequireConfirmedAccount && !(await _confirmation.IsConfirmedAsync(UserManager, user)))
        {
            Logger.LogWarning(4, "User cannot sign in without a confirmed account.");
            return false;
        }
        return true;
    }

因此,我建议您可以检查startup.cs,以确保已将电话或RequireConfirmedAccount设置为false(如下所示),如果启用了其他必需信息,则应在应用程序中进行设置.

So I suggest you could check your startup.cs to make sure you have set the phone or RequireConfirmedAccount to false like below, if you have enabled other required information you should set it in your application.

此外,默认情况下,用户名也是电子邮件格式,请修改代码以将用户名也设置为电子邮件". UserName = testUser +"@ gmail.com",

Besides, by default the username is also a Email format, please modify your codes to set username to Email also. UserName = testUser + "@gmail.com",

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDatabaseDeveloperPageExceptionFilter();

        services.AddDefaultIdentity<IdentityUser>(options =>
        { options.SignIn.RequireConfirmedAccount = false;
            options.SignIn.RequireConfirmedEmail = true;
            options.SignIn.RequireConfirmedPhoneNumber = false;

        })
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddControllersWithViews();
    }

这篇关于在ASP.NET Core 5中创建身份验证用户时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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