Cookie的身份验证问题ASP.NET 2.1 CORE。 AWS负载均衡

问题描述 投票:0回答:1

更新:它看起来像下面的代码失败:

services.AddDataProtection()
            .SetApplicationName(appname)
            .PersistKeysToRedis(redis, "DataProtectionKeys")
            .ProtectKeysWithCertificate(LoadCert(Configuration));

这是无法从PFX文件中读取证书。

UPDATE2:噢,我的!证书文件已被排除的.gitignore!:))生活与学习。至少我们住吧;!?))


最初的问题:我有ASP.NET 2.1的核心应用程序部署在背后泊坞容器AWS负载平衡器。当我试图登录到从登录页面的应用程序。我得到InvalidOperationException异常与此理由:

没有authenticationScheme指定,也没有发现DefaultChallengeScheme。

但是,当我再次命中同一个URL,它实际上移动到正确的页面和工作了一段时间,然后再抛出,HTTP状态500和第2次尝试打开它成功相同的页面后相同的异常。有趣的是Chrome是不一样强大IE:如果IE无法异常后恢复浏览器总是在随后提交的页面,产生上述异常返回404。

所以,我将不胜感激,如果有人将能够提供给我的想法如何纠正这种情况很明显的问题是相关的认证,但我无法弄清楚到底应该做什么。

下面是从ConfigureServices()在Startup.cs相关使出:

        string appname = "MyApp";
        var redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("RedisConnection"));
        services.AddDataProtection()
            .SetApplicationName(appname)
            .PersistKeysToRedis(redis, "DataProtectionKeys")
            .ProtectKeysWithCertificate(LoadCert(Configuration));

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
                    options =>
                    { 
                        options.LoginPath = new PathString("/Area/Ctrl/Login");
                        options.LogoutPath = new PathString("/Area/Ctrl/Logout");
                        options.Cookie.IsEssential = true;
                    });

        services.AddDistributedRedisCache(o =>
        {
            o.Configuration = Configuration.GetConnectionString("RedisConnection");
        });
        services.AddSession(options =>
        {
            options.Cookie.Name = appname;
            options.IdleTimeout = TimeSpan.FromSeconds(600);
        });

下面是从Startup.cs配置()相关的代码:

        app.UseForwardedHeaders();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseAuthentication();
        app.UseSession();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
             name: "areas",
             template: "{area:exists}/{controller=Ctrl}/{action=Login}/{id?}"
           );
        }); 

这里是我如何设置在主控制器,它处理登录:

            ClaimsIdentity identity = new ClaimsIdentity(GetUserRoleClaims(dbUserData), CookieAuthenticationDefaults.AuthenticationScheme);
            ClaimsPrincipal principal = new ClaimsPrincipal(identity);
            if (principal == null)
                throw new ApplicationException($"Could not create principal for {user?.UserName} user.");

            await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
            if (httpContext.User == null)
            {
                httpContext.User = principal;
            }
c# amazon-web-services authentication asp.net-core-2.1
1个回答
1
投票

OK,一切现在工作:)这是很大的影响:

  1. 如果您的应用负载均衡下的所有实例具有共享数据保护加密密钥(例如,使用相同的密钥环)。由此而来。Redis的和证书。会议也应共享。由此而来。Redis的一次。
  2. 证书ProtectKeysWithCertificate()调用应该正确加载。如果无法加载不打这通电话可言,但是这将是非常糟糕的主意。只要弄清楚为什么它不加载。
  3. 为了避免出现InvalidOperationException在定制认证的HttpContext被抛出。用户需要手动登录动作中进行分配。

有关证书一件重要的事情:数据保护模块只支持与CAPI私钥证书。 CNG的人被留了下来。

© www.soinside.com 2019 - 2024. All rights reserved.