【发布时间】:2021-11-09 09:37:56
【问题描述】:
我正在使用 .NET 5.0 实现一个 ASP.NET Core webb 应用程序。我们想设置自己的 cookie 名称,但我找不到如何实现。
在将 ITfoxtec SAML 2.0 与 .NET 5.0 结合使用时,有什么方法可以设置您自己的 cookie 名称?
【问题讨论】:
我正在使用 .NET 5.0 实现一个 ASP.NET Core webb 应用程序。我们想设置自己的 cookie 名称,但我找不到如何实现。
在将 ITfoxtec SAML 2.0 与 .NET 5.0 结合使用时,有什么方法可以设置您自己的 cookie 名称?
【问题讨论】:
ITfoxtec Identity SAML 2.0 包尽可能使用 .NET 基础架构。身份验证 cookie 由 ASP.Net 核心身份验证处理。
您需要实现自己的Saml2ServiceCollectionExtensions 版本并设置o.Cookie.Name = "somenewname"。
像这样:
public static IServiceCollection AddSaml2(this IServiceCollection services, string loginPath = "/Auth/Login", bool slidingExpiration = false, string accessDeniedPath = null, ITicketStore sessionStore = null, SameSiteMode cookieSameSite = SameSiteMode.Lax, string cookieDomain = null)
{
services.AddAuthentication(Saml2Constants.AuthenticationScheme)
.AddCookie(Saml2Constants.AuthenticationScheme, o =>
{
o.Cookie.Name = "somenewname";
o.LoginPath = new PathString(loginPath);
o.SlidingExpiration = slidingExpiration;
if(!string.IsNullOrEmpty(accessDeniedPath))
{
o.AccessDeniedPath = new PathString(accessDeniedPath);
}
if (sessionStore != null)
{
o.SessionStore = sessionStore;
}
o.Cookie.SameSite = cookieSameSite;
if (!string.IsNullOrEmpty(cookieDomain))
{
o.Cookie.Domain = cookieDomain;
}
});
return services;
}
【讨论】: