ASP.NET Core Identity - 使令牌(电子邮件确认、密码重置等)在不同服务器上有效?

QTo*_*Tom 6 c# asp.net-identity asp.net-core identityserver4 asp.net-core-identity

我正在使用 ASP.NET Core Identity 和 IdentityServer4 创建身份验证服务。一切正常,但我遇到了一个问题,因为在我们的实时环境中它托管在多台服务器上,在这种情况下,在一台服务器上生成的令牌(例如密码重置令牌)无效另外一个。

我正在设置这样的身份(自定义用户存储等在其他地方注册):

 services.AddDataProtection()
     .SetApplicationName("IdentityService");

 services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
 {
     options.Password.RequireNonAlphanumeric = false;
 })
     .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

如您所见,我已尝试设置应用程序名称,使其在每台服务器上都相同,但这并没有解决问题。

我发现我可以创建一个 IXmlRepository来存储 Identity 使用的密钥:

services.AddDataProtection()
     .SetApplicationName("IdentityService");
     .AddKeyManagementOptions(s => s.XmlRepository = new CustomXmlRepo());
Run Code Online (Sandbox Code Playgroud)

如果我实现这个并将密钥存储在我们的数据库中,这能解决我的问题吗?有没有其他(更好)的方法?

mac*_*kie 1

本文介绍了为实现生产做好准备所需要做的事情identityserver4- 其中一项要求是数据保护密钥的共享和持久存储:

IdentityServer4指南:http://docs.identityserver.io/en/latest/topics/deployment.html

配置 ASP.NET Core 数据保护:https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview ?view=aspnetcore-3.1

实现您自己的方法绝对没问题 - 只要确保它们存储的位置受到充分保护即可。