![](/img/trans.png)
[英]ASP.NET Core 2.0 Preview 1: How to set up Cookie Authentication with custom login path
[英]How to Set Up CORS enabled domain authentication cookie .Net Core?
我正在尝试对 CORS 源请求进行身份验证,并与内部公司单点登录实用程序的用户一起设置声明原则。 到目前为止,我有当前设置,cookie 永远不会在身份验证设置中设置的域上创建。
我有一个 Angular 客户端应用程序和.Net Core 3.0 Webapi,要求客户端能够为未来的 api 调用设置身份验证。
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.Cookie.Name = "access_token";
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.Domain = "localhost:xxxx";
});
//CORS
services.AddCors(options =>
{
options.AddPolicy(
"AllowOrigin",
builder => builder.WithOrigins("localhost:xxxx")
.AllowCredentials()
.AllowAnyHeader()
.AllowAnyMethod());
});
//Sign In
HttpContext.SignInAsync(
scheme: CookieAuthenticationDefaults.AuthenticationScheme,
principal: new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme)),
properties: new AuthenticationProperties { ExpiresUtc = DateTime.Now.Add(120) });
我正在本地测试这一切,所以两个 URL 都是具有不同端口的 localhost Angular 托管: http://localhost:xxxx
WebAPi 托管: http://localhost:xxx2
从 Angular 到 webapi 的 http 请求是http://localhost:xxx2/api/auth
,它具有 SignInAsync 调用,公司单点登录不会创建用户名,而是 cookie。 如果我删除options.Cookie.Domain = "localhost:xxxx";
cookie 确实在 webapi 域http://localhost:xxx2
上创建。 我一定在这里遗漏了一些东西。
在阅读了一些关于 stackoverflow 的其他帖子后,它发现 AllowAllOrigins 只会解决这个问题,但会构成威胁。
所以我最终用 JWT 解决了这个问题 - 为从客户端接口发送的每个请求设置授权令牌。 这个问题是由于客户端和 WebApi 托管在不同的域上造成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.