Remove redundant setters for CookieAuthSchemes

This commit is contained in:
Hao Kung 2016-02-12 10:57:52 -08:00
parent 57307ef2b7
commit 06f4c306b7
3 changed files with 21 additions and 17 deletions

View File

@ -48,7 +48,6 @@ namespace IdentitySample
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>(options => { services.AddIdentity<ApplicationUser, IdentityRole>(options => {
options.Cookies.ApplicationCookieAuthenticationScheme = "ApplicationCookie";
options.Cookies.ApplicationCookie.AuthenticationScheme = "ApplicationCookie"; options.Cookies.ApplicationCookie.AuthenticationScheme = "ApplicationCookie";
options.Cookies.ApplicationCookie.DataProtectionProvider = new DataProtectionProvider(new DirectoryInfo("C:\\Github\\Identity\\artifacts")); options.Cookies.ApplicationCookie.DataProtectionProvider = new DataProtectionProvider(new DirectoryInfo("C:\\Github\\Identity\\artifacts"));
options.Cookies.ApplicationCookie.CookieName = "Interop"; options.Cookies.ApplicationCookie.CookieName = "Interop";

View File

@ -13,12 +13,17 @@ namespace Microsoft.AspNetCore.Identity
/// </summary> /// </summary>
public class IdentityCookieOptions 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() public IdentityCookieOptions()
{ {
// Configure all of the cookie middlewares // Configure all of the cookie middlewares
ApplicationCookie = new CookieAuthenticationOptions ApplicationCookie = new CookieAuthenticationOptions
{ {
AuthenticationScheme = ApplicationCookieAuthenticationScheme, AuthenticationScheme = DefaultApplicationScheme,
AutomaticAuthenticate = true, AutomaticAuthenticate = true,
AutomaticChallenge = true, AutomaticChallenge = true,
LoginPath = new PathString("/Account/Login"), LoginPath = new PathString("/Account/Login"),
@ -30,21 +35,21 @@ namespace Microsoft.AspNetCore.Identity
ExternalCookie = new CookieAuthenticationOptions ExternalCookie = new CookieAuthenticationOptions
{ {
AuthenticationScheme = ExternalCookieAuthenticationScheme, AuthenticationScheme = DefaultExternalScheme,
CookieName = ExternalCookieAuthenticationScheme, CookieName = DefaultExternalScheme,
ExpireTimeSpan = TimeSpan.FromMinutes(5) ExpireTimeSpan = TimeSpan.FromMinutes(5)
}; };
TwoFactorRememberMeCookie = new CookieAuthenticationOptions TwoFactorRememberMeCookie = new CookieAuthenticationOptions
{ {
AuthenticationScheme = TwoFactorRememberMeCookieAuthenticationScheme, AuthenticationScheme = DefaultTwoFactorRememberMeScheme,
CookieName = TwoFactorRememberMeCookieAuthenticationScheme CookieName = DefaultTwoFactorRememberMeScheme
}; };
TwoFactorUserIdCookie = new CookieAuthenticationOptions TwoFactorUserIdCookie = new CookieAuthenticationOptions
{ {
AuthenticationScheme = TwoFactorUserIdCookieAuthenticationScheme, AuthenticationScheme = DefaultTwoFactorUserIdScheme,
CookieName = TwoFactorUserIdCookieAuthenticationScheme, CookieName = DefaultTwoFactorUserIdScheme,
ExpireTimeSpan = TimeSpan.FromMinutes(5) ExpireTimeSpan = TimeSpan.FromMinutes(5)
}; };
@ -56,27 +61,27 @@ namespace Microsoft.AspNetCore.Identity
public CookieAuthenticationOptions TwoFactorUserIdCookie { get; set; } public CookieAuthenticationOptions TwoFactorUserIdCookie { get; set; }
/// <summary> /// <summary>
/// Gets or sets the scheme used to identify application authentication cookies. /// Gets the scheme used to identify application authentication cookies.
/// </summary> /// </summary>
/// <value>The scheme used to identify application authentication cookies.</value> /// <value>The scheme used to identify application authentication cookies.</value>
public string ApplicationCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".Application"; public string ApplicationCookieAuthenticationScheme => ApplicationCookie?.AuthenticationScheme;
/// <summary> /// <summary>
/// Gets or sets the scheme used to identify external authentication cookies. /// Gets the scheme used to identify external authentication cookies.
/// </summary> /// </summary>
/// <value>The scheme used to identify external authentication cookies.</value> /// <value>The scheme used to identify external authentication cookies.</value>
public string ExternalCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".External"; public string ExternalCookieAuthenticationScheme => ExternalCookie?.AuthenticationScheme;
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <value>The scheme used to identify user identity 2fa authentication cookies.</value> /// <value>The scheme used to identify user identity 2fa authentication cookies.</value>
public string TwoFactorUserIdCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".TwoFactorUserId"; public string TwoFactorUserIdCookieAuthenticationScheme => TwoFactorUserIdCookie?.AuthenticationScheme;
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <value>The scheme used to identify remember me application authentication cookies.</value> /// <value>The scheme used to identify remember me application authentication cookies.</value>
public string TwoFactorRememberMeCookieAuthenticationScheme { get; set; } = typeof(IdentityCookieOptions).Namespace + ".TwoFactorRememberMe"; public string TwoFactorRememberMeCookieAuthenticationScheme => TwoFactorRememberMeCookie?.AuthenticationScheme;
} }
} }

View File

@ -503,7 +503,7 @@ namespace Microsoft.AspNetCore.Identity.Test
{ {
// Setup // Setup
var manager = MockHelpers.TestUserManager<TestUser>(); var manager = MockHelpers.TestUserManager<TestUser>();
manager.Options.Cookies.ApplicationCookieAuthenticationScheme = authenticationScheme; manager.Options.Cookies.ApplicationCookie.AuthenticationScheme = authenticationScheme;
var context = new Mock<HttpContext>(); var context = new Mock<HttpContext>();
var auth = new Mock<AuthenticationManager>(); var auth = new Mock<AuthenticationManager>();
context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();