【问题标题】:How do I set cookie to be secure .NET Core?如何将 cookie 设置为安全的 .NET Core?
【发布时间】:2020-07-01 14:08:01
【问题描述】:
我在使用 Identity Server 4 时遇到了问题。这是需要 SameSite=None 和安全 cookie 的常见问题。虽然我设法将 SameSite 设置为 None,但我并没有将 cookie 设置为安全。
我尝试在线搜索,但找不到任何完整的解决方案。因为我没有经验,所以我也需要知道在哪里使用这部分代码。
我正在使用 ASP.NET Core 3.1。
谢谢
【问题讨论】:
标签:
security
asp.net-core
identityserver4
asp.net-core-3.0
asp.net-core-3.1
【解决方案1】:
您可以像这样设置 cookie 设置
services.AddIdentityServer()
.AddInMemoryClients(Clients.Get())
.AddInMemoryIdentityResources(Resources.GetIdentityResources())
.AddInMemoryApiResources(Resources.GetApiResources())
.AddDeveloperSigningCredential()
.AddTestUsers(TestUsers.Users);
services.AddAuthentication("MyCookie")
.AddCookie("MyCookie", options =>
{
options.ExpireTimeSpan = ...;
});
更多信息请阅读Cookie authentication。