MVC ResetPasswordAsync 不适用于不同的网站

MVC ResetPasswordAsync not working across different website

我有 2 个具有完全相同代码的 Web 应用程序,只是在 Azure 中部署了不同的身份验证方法,即站点 A(表单身份验证)和站点 B(windows 身份验证)

这是生成令牌的代码段,我在 2 个不同的控制器中有相同的代码 帐户和管理员重置

var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var encoded = HttpUtility.UrlEncode(code);

我重置密码的代码是(在帐户控制器中)-

var result = await UserManager.ResetPasswordAsync(model.UserId, HttpUtility.UrlDecode(model.Code), model.Password);
if (result.Succeeded)
                {
                    //do something
                }            

当我以用户管理员身份从站点 B 生成令牌时遇到问题(在站点 A 上生成密码重置 url),当我尝试在站点 A 上重置密码时,提示token无效

如果我以普通用户身份在站点 A 上生成令牌并在站点 A 中重置它,它会正常工作..

是不是因为token是跨网站生成的?

当您使用 GeneratePasswordResetTokenAsync 调用生成令牌时,ASP.NET Identity 使用内置 Data Protection APIs 创建加密令牌。

数据保护 API 使用“密钥环”来加密和解密令牌。默认情况下,此“密钥环”存储在本地服务器文件系统中。此行为记录在案 here.

The ASP.NET Core Data Protection system is used by apps to protect data. Data Protection relies upon a set of cryptographic keys stored in a key ring. When the Data Protection system is initialized, it applies default settings that store the key ring locally. Under the default configuration, a unique key ring is stored on each node of the web farm. Consequently, each web farm node can't decrypt data that's encrypted by an app on any other node.

在您的场景中,由于重置令牌是在站点 B 中生成,然后在站点 A 中使用,因此无法解密令牌。站点 A 不知道如何解密站点 B 生成的令牌,因为它们不共享相同的“密钥环”。

为了解决这个问题,您需要将站点 A 和站点 B 配置为使用中央(安全)位置来存储“密钥环”,如概述的那样 here.