简体   繁体   English

在ASP.NET Core中的应用程序和子应用程序之间共享Cookie

[英]Share Cookie between application and sub application in ASP.NET Core

I am looking for a way to share a login screen between a root application and a sub application (Virtual Directory configured as an Application) in ASP.NET Core. 我正在寻找一种在ASP.NET Core中的根应用程序和子应用程序(配置为应用程序的虚拟目录)之间共享登录屏幕的方法。 The LoginPath for the sub application will be pointing to the root login page (which the returnUrl will then point back to the sub application). 子应用程序的LoginPath将指向根登录页面(然后returnUrl将其指向子应用程序)。 Basically, I need the sub application to recognize the credentials set by the root application. 基本上,我需要子应用程序来识别由根应用程序设置的凭据。 I am using cookie authentication. 我正在使用cookie身份验证。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationScheme = "Cookie",
    LoginPath = new PathString("./signin/"),
    AccessDeniedPath = new PathString("/unauthorized/"),
    AutomaticAuthenticate = true,
    AutomaticChallenge = true
});

Here is my code issuing the creds. 这是我发出证明的代码。

var issuer = SettingBusiness.GetSettingValueAsString("MembersSiteUrl");
var claims = new List<Claim> {new Claim(ClaimTypes.Name, user.UserId.ToString(), ClaimValueTypes.Integer32, issuer)};
var userIdentity = new ClaimsIdentity("MembersUser");
userIdentity.AddClaims(claims);
var userPrincipal = new ClaimsPrincipal(userIdentity);
var authenticationProperties = new AuthenticationProperties
{`enter code here`
    ExpiresUtc = DateTime.UtcNow.AddMinutes(60),
    IsPersistent = false,
    AllowRefresh = true
};
await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, authenticationProperties);

Having a hard time finding info on this particular scenario. 很难找到有关此特定方案的信息。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I assume this cookie is encrypted with the new data protection system . 我认为此Cookie已使用新的数据保护系统加密。

Your root application will encrypt the cookie with its private key. 您的根应用程序将使用其私钥对cookie进行加密。 Your sub-application currently does not have access to this private key and won't be able to decrypt this cookie. 您的子应用程序当前无权访问此私钥,并且将无法解密此Cookie。

In order for your sub-application to be able to decrypt the cookie, you will need to setup a shared key storage provider . 为了使您的子应用程序能够解密cookie,您将需要设置一个共享密钥存储提供程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 共享 cookie .net Core 3 和 Asp.net - Share cookie .net Core 3 and Asp.net ASP.NET Core身份:是否希望保留应用程序cookie? - ASP.NET Core Identity: intended that application cookie remains? 在ASP.NET Core Web应用程序的浏览器中未生成Cookie - Cookie not generated in browser on ASP.NET Core Web Application Cookie 身份验证在 ASP.NET Core 应用程序中不起作用 - Cookie authentication is not working in ASP.NET Core application asp.net 内核中的控制台应用程序和 Web 应用程序之间的区别 - Difference between a console application and Web application in asp.net core 如何在PHP和ASP.net应用程序之间共享会话? - How to share sessions between PHP and ASP.net application? 在 ASP.NET Core 2.2 和 ASP 之间共享 Cookie 身份验证。 NET MVC 5 (.NET Framework 4.6.1) 没有 Microsoft.Identity - Share Cookie authentication between ASP.NET Core 2.2 and ASP. NET MVC 5 (.NET Framework 4.6.1) without Microsoft.Identity 如何在ASP.NET MVC 5应用程序和.NET控制台应用程序之间共享数据库? - How to share a database between ASP.NET MVC 5 application and .NET console application? 主应用程序中的 ASP.NET Core 3.1 Host Sub 应用程序 - ASP.NET Core 3.1 Host Sub application within main application 如何在父控制台应用程序和ASP.NET Web应用程序之间共享app.config - How to share app.config between parent console application and ASP.NET web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM