【问题标题】:Share Authentication Cookie between .net 4x and .net 5 app - Accessing the User ClaimsPrinciple在 .net 4x 和 .net 5 应用程序之间共享身份验证 Cookie - 访问用户 ClaimsPrincipal
【发布时间】:2021-03-20 23:20:04
【问题描述】:

我已按照本文建议的设置进行操作 https://docs.microsoft.com/en-us/aspnet/core/security/cookie-sharing?view=aspnetcore-5.0#share-authentication-cookies-between-aspnet-4x-and-aspnet-core-apps

我通过登录 .net 4x 应用程序设置身份验证 cookie,然后尝试访问由 .net 5 核心应用程序上的 [Authorize] 属性保护的页面类。这失败了,我被引导回登录页面。如果我删除 [Authorize] 属性并访问和解密共享身份验证 cookie,我可以在 AuthenticationTicket 中看到由 .net 4x 应用程序创建的用户和声明(请参见下面的代码) - 但是当尝试访问 ClaimsPrincipal 用户时这页纸。此用户没有 cookie 的任何详细信息。 var user = _userManager.GetUserAsync(User).Result; 始终为空。

var dataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\temp\keyring"));         
            var cookieManager = new ChunkingCookieManager();
            var cookie = cookieManager.GetRequestCookie(HttpContext, ".AspNetCore.SharedCookie");
            var dataProtector = dataProtectionProvider.CreateProtector
("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Identity.Application", "v2");
            //Get teh decrypted cookies as a Authentication Ticket
            TicketDataFormat ticketDataFormat = new TicketDataFormat(dataProtector);
            AuthenticationTicket ticket = ticketDataFormat.Unprotect(cookie);

            var user =  _userManager.GetUserAsync(User).Result;

下面是.net 5应用中startup.cs文件中的configureservices代码

....

 services.AddDbContext<RosterDBContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("databaseconnection")
                    , sqlServerOptions => sqlServerOptions.CommandTimeout(120)
                    );
                options.EnableSensitiveDataLogging();
            });

            services.AddIdentity<RosterUser, RosterRole>()
                .AddEntityFrameworkStores<RosterDBContext>();
                

            services.Configure<SecurityStampValidatorOptions>
                (
                    options => { 
                        options.ValidationInterval = TimeSpan.FromSeconds(10);                        
                    }
                );

            services.AddDataProtection()
                 .PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp\keyring"));               

            services.AddAuthentication("Identity.Application")
                .AddCookie(config =>
                {
                    config.Cookie.Name = ".AspNetCore.SharedCookie";
                    config.Cookie.Path = "/";
                });

            services.ConfigureApplicationCookie(options =>
            {              
                options.LoginPath = "/Identity/Account/Login/";                   

            });
            services.AddHttpContextAccessor();
            services.AddRazorPages();

【问题讨论】:

    标签: c# .net asp.net-core authentication cookies


    【解决方案1】:

    对于看到这一点的任何人 - 我能够解决这个问题。我删除了这些行并替换了services.AddIdentity&lt;RosterUser, RosterRole&gt;() .AddEntityFrameworkStores&lt;RosterDBContext&gt;();

    以下内容:

    services.AddIdentityCore&lt;IdentityUser&gt;().AddRoles&lt;IdentityRole&gt;().AddSignInManager() .AddEntityFrameworkStores&lt;RosterDbContext&gt;();

    我删除了我自己的身份用户以消除由于用户引起的任何问题,并使用了 AddIdentityCore 方法而不是 AddIdentity 。这停止了​​我一直在声明“方案已经存在:Identity.Application”的错误,因为这条线.AddCookie("Identity.Application"

    我还向两个应用程序添加了应用程序名称: .net 4x ,(builder) =&gt; { builder.SetApplicationName("cms-app"); }) 和 .net 核心应用程序 .PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp\keyring")) .SetApplicationName("cms-app");

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      相关资源
      最近更新 更多