gpt4 book ai didi

c# - 每次使用 "Not Allowed"使用 ASP Identity 登录失败(即使 'email' 和 'username' 具有相同的值))

转载 作者:太空狗 更新时间:2023-10-29 20:49:08 24 4
gpt4 key购买 nike

注册用户工作正常,因为它通过此行登录:

await _signInManager.SignInAsync(user, isPersistent: model.RememberMe);

但是如果我想再次使用相同的用户登录是行不通的(任务结果总是返回“不允许”)。

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);

我确实意识到 PasswordSignInAsync 需要一个 USERNAME,而不是一个 EMAIL - 但它们对于我的应用程序是一样的。

我尝试了各种登录方法,包括检查用户和密码是否正确(全部成功)、传入用户对象、更改 ViewModel 属性名称等。

有什么想法需要改变吗?

上下文的类似问题:ASP.NET Identity Provider SignInManager Keeps Returning Failure

谢谢。

最佳答案

好的,我想通了。我在这里查看了源代码 - https://github.com/aspnet/Identity/blob/dev/src/Identity/SignInManager.cs .

NotAllowed 只在这里设置:

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;
}

所以我深入研究了 CanSignInAsync...

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

哦,我知道这是怎么回事。我们来看看我的 Startup.cs 配置。

services.Configure<IdentityOptions>(options =>
{
...
options.SignIn.RequireConfirmedEmail = true;
...
}

哦,亲爱的,好的。

我所要做的就是弹出数据库并将我的用户设置为 EmailConfirmed = true。 PEBCAK .

“不允许”是有道理的,但没有随它返回的错误消息 - 所以这不是了解发生了什么的最佳方式。幸运的是,.NET Core 很容易深入研究源代码。

关于c# - 每次使用 "Not Allowed"使用 ASP Identity 登录失败(即使 'email' 和 'username' 具有相同的值)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com