gpt4 book ai didi

c# - Identity Core TwoFactorSignIn 是否包含错误?

转载 作者:行者123 更新时间:2023-11-30 17:36:19 25 4
gpt4 key购买 nike

几个月来我一直在开发 ASP.NET Core 应用程序。现在接近完成第一个测试版时,我意识到我没有启用双因素身份验证,现在我想我发现了 Microsoft.AspNetCore.Identity 实现中的一个错误。如果我们查看如何检索用户,它会执行以下操作:

    /// <summary>
/// Returns the User ID claim value if present otherwise returns null.
/// </summary>
/// <param name="principal">The <see cref="ClaimsPrincipal"/> instance.</param>
/// <returns>The User ID claim value, or null if the claim is not present.</returns>
/// <remarks>The User ID claim is identified by <see cref="ClaimTypes.NameIdentifier"/>.</remarks>
public virtual string GetUserId(ClaimsPrincipal principal)
{
if (principal == null)
{
throw new ArgumentNullException(nameof(principal));
}
return principal.FindFirstValue(Options.ClaimsIdentity.UserIdClaimType);
}

/// <summary>
/// Returns the user corresponding to the IdentityOptions.ClaimsIdentity.UserIdClaimType claim in
/// the principal or null.
/// </summary>
/// <param name="principal">The principal which contains the user id claim.</param>
/// <returns>The user corresponding to the IdentityOptions.ClaimsIdentity.UserIdClaimType claim in
/// the principal or null</returns>
public virtual Task<TUser> GetUserAsync(ClaimsPrincipal principal)
{
if (principal == null)
{
throw new ArgumentNullException(nameof(principal));
}
var id = GetUserId(principal);
return id == null ? Task.FromResult<TUser>(null) : FindByIdAsync(id);
}

但是,SignInManager 中的TwoFactorSignInAsync 方法从不设置 UserIdClaimType 类型的声明,但它设置了 4 次相同的 名称声明,包含用户的Id
这是 TwoFactorSignInAsync 实现中的错误,还是我的 Identity 配置中的某些配置不正确?这是哪个:

CookieAuthenticationOptions cookieOptions = new CookieAuthenticationOptions
{
CookieHttpOnly = true,
LoginPath = "/User/Login",
CookieSecure = CookieSecurePolicy.Always,
LogoutPath = "/User/Logout"
};

services.AddIdentity<User, Role>(options =>
{
options.Cookies.ApplicationCookie = cookieOptions;
options.Cookies.ExternalCookie = cookieOptions;
options.Cookies.TwoFactorRememberMeCookie = cookieOptions;
options.Cookies.TwoFactorUserIdCookie = cookieOptions;

options.Password = new PasswordOptions
{
RequiredLength = 8,
RequireLowercase = true,
RequireUppercase = true,
RequireNonAlphanumeric = true
};

options.SignIn.RequireConfirmedEmail = true;
})
.AddUserStore<MyStore>()
.AddRoleStore<MyStore>()
.AddDefaultTokenProviders();

GitHub问题请看Does TwoFactorSignIn contain a bug or am I configuring Identity incorrectly? #981

最佳答案

根据 @HaoK's comment :

Two factor sign in if successful, means the NEXT request will have the User set. Authentication for the current request has already happened. None of the SignIn's have no effect on the current request.

解决方案是在调用 TwoFactorSignInAsync 之后删除 GetCurrentUserAsync 方法,我错误地认为它会立即登录用户。

关于c# - Identity Core TwoFactorSignIn 是否包含错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39746258/

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