diff --git a/samples/IdentitySample.Mvc/Startup.cs b/samples/IdentitySample.Mvc/Startup.cs index 02fbbc35d9..7a4a74d08a 100644 --- a/samples/IdentitySample.Mvc/Startup.cs +++ b/samples/IdentitySample.Mvc/Startup.cs @@ -48,7 +48,6 @@ namespace IdentitySample options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); services.AddIdentity(options => { - options.Cookies.ApplicationCookieAuthenticationScheme = "ApplicationCookie"; options.Cookies.ApplicationCookie.AuthenticationScheme = "ApplicationCookie"; options.Cookies.ApplicationCookie.DataProtectionProvider = new DataProtectionProvider(new DirectoryInfo("C:\\Github\\Identity\\artifacts")); options.Cookies.ApplicationCookie.CookieName = "Interop"; diff --git a/src/Microsoft.AspNetCore.Identity/IdentityCookieOptions.cs b/src/Microsoft.AspNetCore.Identity/IdentityCookieOptions.cs index fb07ce5688..8a214faa45 100644 --- a/src/Microsoft.AspNetCore.Identity/IdentityCookieOptions.cs +++ b/src/Microsoft.AspNetCore.Identity/IdentityCookieOptions.cs @@ -13,12 +13,17 @@ namespace Microsoft.AspNetCore.Identity /// public class IdentityCookieOptions { + private static readonly string DefaultApplicationScheme = typeof(IdentityCookieOptions).Namespace + ".Application"; + private static readonly string DefaultExternalScheme = typeof(IdentityCookieOptions).Namespace + ".External"; + private static readonly string DefaultTwoFactorRememberMeScheme = typeof(IdentityCookieOptions).Namespace + ".TwoFactorRememberMe"; + private static readonly string DefaultTwoFactorUserIdScheme = typeof(IdentityCookieOptions).Namespace + ".TwoFactorUserId"; + public IdentityCookieOptions() { // Configure all of the cookie middlewares ApplicationCookie = new CookieAuthenticationOptions { - AuthenticationScheme = ApplicationCookieAuthenticationScheme, + AuthenticationScheme = DefaultApplicationScheme, AutomaticAuthenticate = true, AutomaticChallenge = true, LoginPath = new PathString("/Account/Login"), @@ -30,21 +35,21 @@ namespace Microsoft.AspNetCore.Identity ExternalCookie = new CookieAuthenticationOptions { - AuthenticationScheme = ExternalCookieAuthenticationScheme, - CookieName = ExternalCookieAuthenticationScheme, + AuthenticationScheme = DefaultExternalScheme, + CookieName = DefaultExternalScheme, ExpireTimeSpan = TimeSpan.FromMinutes(5) }; TwoFactorRememberMeCookie = new CookieAuthenticationOptions { - AuthenticationScheme = TwoFactorRememberMeCookieAuthenticationScheme, - CookieName = TwoFactorRememberMeCookieAuthenticationScheme + AuthenticationScheme = DefaultTwoFactorRememberMeScheme, + CookieName = DefaultTwoFactorRememberMeScheme }; TwoFactorUserIdCookie = new CookieAuthenticationOptions { - AuthenticationScheme = TwoFactorUserIdCookieAuthenticationScheme, - CookieName = TwoFactorUserIdCookieAuthenticationScheme, + AuthenticationScheme = DefaultTwoFactorUserIdScheme, + CookieName = DefaultTwoFactorUserIdScheme, ExpireTimeSpan = TimeSpan.FromMinutes(5) }; @@ -56,27 +61,27 @@ namespace Microsoft.AspNetCore.Identity public CookieAuthenticationOptions TwoFactorUserIdCookie { get; set; } /// - /// Gets or sets the scheme used to identify application authentication cookies. + /// Gets the scheme used to identify application authentication cookies. /// /// The scheme used to identify application authentication cookies. - public string ApplicationCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".Application"; + public string ApplicationCookieAuthenticationScheme => ApplicationCookie?.AuthenticationScheme; /// - /// Gets or sets the scheme used to identify external authentication cookies. + /// Gets the scheme used to identify external authentication cookies. /// /// The scheme used to identify external authentication cookies. - public string ExternalCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".External"; + public string ExternalCookieAuthenticationScheme => ExternalCookie?.AuthenticationScheme; /// - /// Gets or sets the scheme used to identify Two Factor authentication cookies for round tripping user identities. + /// Gets the scheme used to identify Two Factor authentication cookies for round tripping user identities. /// /// The scheme used to identify user identity 2fa authentication cookies. - public string TwoFactorUserIdCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".TwoFactorUserId"; + public string TwoFactorUserIdCookieAuthenticationScheme => TwoFactorUserIdCookie?.AuthenticationScheme; /// - /// Gets or sets the scheme used to identify Two Factor authentication cookies for saving the Remember Me state. + /// Gets the scheme used to identify Two Factor authentication cookies for saving the Remember Me state. /// /// The scheme used to identify remember me application authentication cookies. - public string TwoFactorRememberMeCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".TwoFactorRememberMe"; + public string TwoFactorRememberMeCookieAuthenticationScheme => TwoFactorRememberMeCookie?.AuthenticationScheme; } } diff --git a/test/Microsoft.AspNetCore.Identity.Test/SignInManagerTest.cs b/test/Microsoft.AspNetCore.Identity.Test/SignInManagerTest.cs index 780b8ceece..02b44604ef 100644 --- a/test/Microsoft.AspNetCore.Identity.Test/SignInManagerTest.cs +++ b/test/Microsoft.AspNetCore.Identity.Test/SignInManagerTest.cs @@ -503,7 +503,7 @@ namespace Microsoft.AspNetCore.Identity.Test { // Setup var manager = MockHelpers.TestUserManager(); - manager.Options.Cookies.ApplicationCookieAuthenticationScheme = authenticationScheme; + manager.Options.Cookies.ApplicationCookie.AuthenticationScheme = authenticationScheme; var context = new Mock(); var auth = new Mock(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();