负载均衡器中的基本身份验证问题

时间:2019-08-23 18:49:36

标签: authentication asp.net-core

我有一些网络应用,其基本的身份验证基础是存储在appsettings.json中的用户/密码

此功能可在隔离服务器中使用,但具有负载均衡器的服务器场服务器不起作用。

问题是,此后进入页面并在服务器X中登录时,重定向以执行操作(可能是服务器Y响应),并且他检测到未登录并重定向至登录,在某些情况下,如果是同一服务器响应,则此操作将继续正常。

它对cookie进行身份验证并使用HttpContext.SignInAsync

我认为这个问题是针对具有不同序列号或类似名称的某些服务器读取的cookie,但随后我将AddDataProtection用于设置ApplicationName仍然无法正常工作。

ConfigureServices

 services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        // configure basic authentication 
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();

        // configure ApplicationName for cookie, for load balancer purpose
        services.AddDataProtection()
            .SetApplicationName("FooTest");

        services.AddMvc(options => options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

配置

if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseCookiePolicy();

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Users}/{action=Login}");
        });

登录名

var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, userResult.Username)
            }, CookieAuthenticationDefaults.AuthenticationScheme);

            var principal = new ClaimsPrincipal(identity);

            var authProperties = new AuthenticationProperties
            {
                IsPersistent = user.IsPersistent,
                ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(_appValues.CookieExpirationTime)
            };

            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, authProperties);

所以解决方案是,共享ServerX和ServerY中的cookie身份验证可以读取,或者是这里的另一种解决方案。

有什么主意吗?

致谢。

0 个答案:

没有答案