New: Use instance name in forms authentication cookie name

(cherry picked from commit 97ebaf279650082c6baee9563ef179921c5ed25a)

Closes #5102
pull/5106/head
Mark McDowall 4 months ago committed by Bogdan
parent 60fe75877b
commit 2818f4e073

@ -1,7 +1,10 @@
using System; using System;
using System.Web;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NzbDrone.Core.Authentication; using NzbDrone.Core.Authentication;
using NzbDrone.Core.Configuration;
namespace Lidarr.Http.Authentication namespace Lidarr.Http.Authentication
{ {
@ -29,19 +32,25 @@ namespace Lidarr.Http.Authentication
public static AuthenticationBuilder AddAppAuthentication(this IServiceCollection services) public static AuthenticationBuilder AddAppAuthentication(this IServiceCollection services)
{ {
return services.AddAuthentication() services.AddOptions<CookieAuthenticationOptions>(AuthenticationType.Forms.ToString())
.AddNone(AuthenticationType.None.ToString()) .Configure<IConfigFileProvider>((options, configFileProvider) =>
.AddExternal(AuthenticationType.External.ToString())
.AddBasic(AuthenticationType.Basic.ToString())
.AddCookie(AuthenticationType.Forms.ToString(), options =>
{ {
options.Cookie.Name = "LidarrAuth"; // Url Encode the cookie name to account for spaces or other invalid characters in the configured instance name
var instanceName = HttpUtility.UrlEncode(configFileProvider.InstanceName);
options.Cookie.Name = $"{instanceName}Auth";
options.AccessDeniedPath = "/login?loginFailed=true"; options.AccessDeniedPath = "/login?loginFailed=true";
options.LoginPath = "/login"; options.LoginPath = "/login";
options.ExpireTimeSpan = TimeSpan.FromDays(7); options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.SlidingExpiration = true; options.SlidingExpiration = true;
options.ReturnUrlParameter = "returnUrl"; options.ReturnUrlParameter = "returnUrl";
}) });
return services.AddAuthentication()
.AddNone(AuthenticationType.None.ToString())
.AddExternal(AuthenticationType.External.ToString())
.AddBasic(AuthenticationType.Basic.ToString())
.AddCookie(AuthenticationType.Forms.ToString())
.AddApiKey("API", options => .AddApiKey("API", options =>
{ {
options.HeaderName = "X-Api-Key"; options.HeaderName = "X-Api-Key";

Loading…
Cancel
Save