使用 2FA 进行密码重置
Using 2FA for password reset
我的应用程序使用 Asp.Net 身份并在 登录 时向我的 Auth 应用程序发送一个双因素代码。这是非常标准的(因为网上有很多示例)并使用 SendCode() 方法。我的理解是 'magic' 是通过这一行完成的:
// Generate the token and send it
if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
{
View("Error");
}
我的要求是确保用户在登录后更改密码。
时要经过相同的 2FA 过程
我的问题是,当执行发送 2FA 代码的代码时:
if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
{
View("Error");
}
我收到错误 'UserID not found':
Server Error in '/MSPortal' Application.
UserId not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: UserId not found.
Source Error:
Line 555:
Line 556: // Generate the token and send it
Line 557: if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
Line 558: {
Line 559: return View("Error");
我知道 SendTwoFactorCodeAsync() 调用 GetVerifiedUserIdAsync(),但我的理解是用户已经过验证,我已经使用 2FA 登录了。
有谁知道我为什么会收到此错误?
谢谢。
我通过重写 IdentityConfig.cs 中的 SendTwoFactorCodeAsync() 解决了这个问题。在此覆盖中,我首先像往常一样调用 GetVerifiedUserIdAsync() 但如果它是 0,我从当前 HttpContext 获取用户 ID。
我并不是说这是最好的方法,但这是我迄今为止所做的,它让我朝着实现 2FA 登录、更改密码和忘记密码的目标前进。
代码(如果我得到反馈,可能会进行一些重构)是:
public override async Task<bool> SendTwoFactorCodeAsync(string provider)
{
int userId = 0;
try
{
userId = await GetVerifiedUserIdAsync();
if (userId == 0)
{
userId = Convert.ToInt32(HttpContext.Current.User.Identity.GetUserId());
}
if (userId == 0)
return false;
}
catch
{
return false;
}
var token = await UserManager.GenerateTwoFactorTokenAsync(userId, provider);
// See IdentityConfig.cs to plug in Email/SMS services to actually send the code
await UserManager.NotifyTwoFactorTokenAsync(userId, provider, token);
return true;
//return base.SendTwoFactorCodeAsync(provider);
}
我的应用程序使用 Asp.Net 身份并在 登录 时向我的 Auth 应用程序发送一个双因素代码。这是非常标准的(因为网上有很多示例)并使用 SendCode() 方法。我的理解是 'magic' 是通过这一行完成的:
// Generate the token and send it
if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
{
View("Error");
}
我的要求是确保用户在登录后更改密码。
时要经过相同的 2FA 过程我的问题是,当执行发送 2FA 代码的代码时:
if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
{
View("Error");
}
我收到错误 'UserID not found':
Server Error in '/MSPortal' Application.
UserId not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: UserId not found.
Source Error:
Line 555:
Line 556: // Generate the token and send it
Line 557: if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
Line 558: {
Line 559: return View("Error");
我知道 SendTwoFactorCodeAsync() 调用 GetVerifiedUserIdAsync(),但我的理解是用户已经过验证,我已经使用 2FA 登录了。
有谁知道我为什么会收到此错误?
谢谢。
我通过重写 IdentityConfig.cs 中的 SendTwoFactorCodeAsync() 解决了这个问题。在此覆盖中,我首先像往常一样调用 GetVerifiedUserIdAsync() 但如果它是 0,我从当前 HttpContext 获取用户 ID。
我并不是说这是最好的方法,但这是我迄今为止所做的,它让我朝着实现 2FA 登录、更改密码和忘记密码的目标前进。
代码(如果我得到反馈,可能会进行一些重构)是:
public override async Task<bool> SendTwoFactorCodeAsync(string provider)
{
int userId = 0;
try
{
userId = await GetVerifiedUserIdAsync();
if (userId == 0)
{
userId = Convert.ToInt32(HttpContext.Current.User.Identity.GetUserId());
}
if (userId == 0)
return false;
}
catch
{
return false;
}
var token = await UserManager.GenerateTwoFactorTokenAsync(userId, provider);
// See IdentityConfig.cs to plug in Email/SMS services to actually send the code
await UserManager.NotifyTwoFactorTokenAsync(userId, provider, token);
return true;
//return base.SendTwoFactorCodeAsync(provider);
}