Machine key in asp.net core 2.0?

前端 未结 1 1215
走了就别回头了
走了就别回头了 2020-12-30 05:49

I have the same asp.net core 2 app running on 2 different servers but using the same database to store users and etc.

The problem is that if I create and set a user

相关标签:
1条回答
  • 2020-12-30 06:10

    You can keep one server as primary and one as secondary. In the secondary server disable auto key generation

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

    Or you can persist them to 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();
    
        // ...
    }
    

    A detailed article is available on the same

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

    PS: The code posted above is from the same articles, so that if link goes the down, the answer is still complete

    0 讨论(0)
提交回复
热议问题