ASP.NET Core cookie authentication - Load balancing (cluster)

Create a Cookie Authentication Cluster Verification

  • MyXmlRepository.csImplement IXmlRepositoryInterface
    public class MyXmlRepository : IXmlRepository
    {
        // 获取所有 XML(物理密钥) 元素
        public IReadOnlyCollection<XElement> GetAllElements()
        {
            // 从数据库读取 XML 数据
        }

        // 存储 XML(物理密钥) 数据
        public void StoreElement(XElement element, string friendlyName)
        {
            // XML保存到数据库
        }

    }
  • Starup.cs Code:
    public void ConfigureServices(IServiceCollection services)
    {
        //...

        // 配置数据保护
        // https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2

       
        services.AddDataProtection()
            .SetApplicationName("SharedCookieApp")
            .Services.Configure<KeyManagementOptions>(options =>
            {
                options.XmlRepository = new MyXmlRepository();
            }); 

        services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie();

        //...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // ...

        app.UseAuthentication();

        //...
    }

Configuration data protection official documents

Guess you like

Origin www.cnblogs.com/cc1027cc/p/11463246.html