diff --git a/src/Microsoft.AspNet.Identity/IUserClaimsPrincipalFactory.cs b/src/Microsoft.AspNet.Identity/IUserClaimsPrincipalFactory.cs index a2d8f7d354..d4c05a8ab8 100644 --- a/src/Microsoft.AspNet.Identity/IUserClaimsPrincipalFactory.cs +++ b/src/Microsoft.AspNet.Identity/IUserClaimsPrincipalFactory.cs @@ -17,6 +17,7 @@ namespace Microsoft.AspNet.Identity /// Creates a from an user asynchronously. /// /// The user to create a from. + /// The name of the authentication method the was sourced from. /// The that represents the asynchronous creation operation, containing the created . Task CreateAsync(TUser user); } diff --git a/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs b/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs index 1f04c23d06..cf6eb29667 100644 --- a/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs +++ b/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs @@ -11,6 +11,36 @@ namespace System.Security.Claims /// public static class PrincipalExtensions { + /// + /// Returns the Name claim value if present otherwise returns null. + /// + /// The instance this method extends. + /// The Name claim value, or null if the claim is not present. + /// The Name claim is identified by . + public static string GetUserName(this ClaimsPrincipal principal) + { + if (principal == null) + { + throw new ArgumentNullException(nameof(principal)); + } + return principal.FindFirstValue(ClaimsIdentity.DefaultNameClaimType); + } + + /// + /// Returns the User ID claim value if present otherwise returns null. + /// + /// The instance this method extends. + /// The User ID claim value, or null if the claim is not present. + /// The User ID claim is identified by . + public static string GetUserId(this ClaimsPrincipal principal) + { + if (principal == null) + { + throw new ArgumentNullException(nameof(principal)); + } + return principal.FindFirstValue(ClaimTypes.NameIdentifier); + } + /// /// Returns true if the principal has an identity with the application cookie identity /// @@ -22,7 +52,7 @@ namespace System.Security.Claims { throw new ArgumentNullException(nameof(principal)); } - return principal.Identities != null && + return principal?.Identities != null && principal.Identities.Any(i => i.AuthenticationType == IdentityCookieOptions.ApplicationCookieAuthenticationType); } diff --git a/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs b/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs index cd538f0933..5f4b98a58a 100644 --- a/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs +++ b/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Identity public virtual async Task ValidateAsync(CookieValidatePrincipalContext context) { var manager = context.HttpContext.RequestServices.GetRequiredService>(); - var userId = context.Principal.FindFirstValue(manager.Options.ClaimsIdentity.UserIdClaimType); + var userId = context.Principal.GetUserId(); var user = await manager.ValidateSecurityStampAsync(context.Principal, userId); if (user != null) { diff --git a/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs b/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs index ce5bac6b2a..528308b26b 100644 --- a/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs @@ -15,9 +15,19 @@ namespace Microsoft.AspNet.Identity.Test public void IdentityNullCheckTest() { ClaimsPrincipal p = null; + Assert.Throws("principal", () => p.GetUserId()); + Assert.Throws("principal", () => p.GetUserName()); Assert.Throws("principal", () => p.FindFirstValue(null)); } + [Fact] + public void UserNameAndIdTest() + { + var p = CreateTestExternalIdentity(); + Assert.Equal("NameIdentifier", p.GetUserId()); + Assert.Equal("Name", p.GetUserName()); + } + [Fact] public void IdentityExtensionsFindFirstValueNullIfUnknownTest() { diff --git a/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs b/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs index 1c8d79e0f1..4c39dc78a4 100644 --- a/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/SecurityStampValidatorTest.cs @@ -40,14 +40,13 @@ namespace Microsoft.AspNet.Identity.Test var userManager = MockHelpers.MockUserManager(); var claimsManager = new Mock>(); var identityOptions = new IdentityOptions { SecurityStampValidationInterval = TimeSpan.Zero }; - identityOptions.ClaimsIdentity.UserIdClaimType = "IdClaim"; var options = new Mock>(); options.Setup(a => a.Value).Returns(identityOptions); var httpContext = new Mock(); var contextAccessor = new Mock(); contextAccessor.Setup(a => a.HttpContext).Returns(httpContext.Object); var id = new ClaimsIdentity(identityOptions.Cookies.ApplicationCookieAuthenticationScheme); - id.AddClaim(new Claim(identityOptions.ClaimsIdentity.UserIdClaimType, user.Id)); + id.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); var principal = new ClaimsPrincipal(id); var properties = new AuthenticationProperties { IssuedUtc = DateTimeOffset.UtcNow, IsPersistent = isPersistent };