在asp.net核心2.0中的机器键?

问题描述:

我有相同的asp.net核心2应用程序运行在2个不同的服务器上,但使用相同的数据库来存储用户等。在asp.net核心2.0中的机器键?

问题是,如果我创建并设置一个服务器的用户密码,另一个运行相同应用程序的服务器返回无效密码,反之亦然。

几年前,我有一个asp.net 4应用程序有这个问题,我通过设置两个应用程序相同的机器键来解决它。

我听说过关于数据保护的api,但我无法找到告诉它使用相同加密密钥的地方,而是发现了令我困惑的复杂示例,我需要的就是让两台服务器都了解彼此的加密。

+0

我的假设是你正在使用Asp.Net Identity。对? –

+0

是的正确,asp.net身份 – user3900456

您可以保留一台服务器作为主服务器,一台作为辅助服务器。在辅助服务器禁用自动密钥生成

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddDataProtection().DisableAutomaticKeyGeneration(); 
} 

或者你可以坚持他们的Redis

public void ConfigureServices(IServiceCollection services) 
{ 
    // sad but a giant hack :(
    // https://github.com/StackExchange/StackExchange.Redis/issues/410#issuecomment-220829614 
    var redisHost = Configuration.GetValue<string>("Redis:Host"); 
    var redisPort = Configuration.GetValue<int>("Redis:Port"); 
    var redisIpAddress = Dns.GetHostEntryAsync(redisHost).Result.AddressList.Last(); 
    var redis = ConnectionMultiplexer.Connect($"{redisIpAddress}:{redisPort}"); 

    services.AddDataProtection().PersistKeysToRedis(redis, "DataProtection-Keys"); 
    services.AddOptions(); 

    // ... 
} 

详细的文章可以在同一

http://www.tugberkugurlu.com/archive/asp-net-core-authentication-in-a-load-balanced-environment-with-haproxy-and-redis

PS:代码上面发布的是来自同一篇文章,所以如果链接失败,答案仍然是完整的

+0

是否有类似的方式来存储它在SQL Server数据库呢? – user3900456

+1

看看这个https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider有帮助,默认情况下你有FileSystem,Azure和注册表驱动程序https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers –