Revert "Remove old 2.0 PrincipalExtensions which are no longer needed"

This reverts commit 1c26e8f5fa.
This commit is contained in:
Hao Kung 2015-12-08 22:49:50 -08:00
parent 1c26e8f5fa
commit 9a2de0f49b
5 changed files with 44 additions and 4 deletions

View File

@ -17,6 +17,7 @@ namespace Microsoft.AspNet.Identity
/// Creates a <see cref="ClaimsPrincipal"/> from an user asynchronously.
/// </summary>
/// <param name="user">The user to create a <see cref="ClaimsPrincipal"/> from.</param>
/// <param name="authenticationType">The name of the authentication method the <paramref name="user"/> was sourced from.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous creation operation, containing the created <see cref="ClaimsPrincipal"/>.</returns>
Task<ClaimsPrincipal> CreateAsync(TUser user);
}

View File

@ -11,6 +11,36 @@ namespace System.Security.Claims
/// </summary>
public static class PrincipalExtensions
{
/// <summary>
/// Returns the Name claim value if present otherwise returns null.
/// </summary>
/// <param name="principal">The <see cref="ClaimsPrincipal"/> instance this method extends.</param>
/// <returns>The Name claim value, or null if the claim is not present.</returns>
/// <remarks>The Name claim is identified by <see cref="ClaimsIdentity.DefaultNameClaimType"/>.</remarks>
public static string GetUserName(this ClaimsPrincipal principal)
{
if (principal == null)
{
throw new ArgumentNullException(nameof(principal));
}
return principal.FindFirstValue(ClaimsIdentity.DefaultNameClaimType);
}
/// <summary>
/// Returns the User ID claim value if present otherwise returns null.
/// </summary>
/// <param name="principal">The <see cref="ClaimsPrincipal"/> instance this method extends.</param>
/// <returns>The User ID claim value, or null if the claim is not present.</returns>
/// <remarks>The User ID claim is identified by <see cref="ClaimTypes.NameIdentifier"/>.</remarks>
public static string GetUserId(this ClaimsPrincipal principal)
{
if (principal == null)
{
throw new ArgumentNullException(nameof(principal));
}
return principal.FindFirstValue(ClaimTypes.NameIdentifier);
}
/// <summary>
/// Returns true if the principal has an identity with the application cookie identity
/// </summary>
@ -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);
}

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Identity
public virtual async Task ValidateAsync(CookieValidatePrincipalContext context)
{
var manager = context.HttpContext.RequestServices.GetRequiredService<SignInManager<TUser>>();
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)
{

View File

@ -15,9 +15,19 @@ namespace Microsoft.AspNet.Identity.Test
public void IdentityNullCheckTest()
{
ClaimsPrincipal p = null;
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserId());
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserName());
Assert.Throws<ArgumentNullException>("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()
{

View File

@ -40,14 +40,13 @@ namespace Microsoft.AspNet.Identity.Test
var userManager = MockHelpers.MockUserManager<TestUser>();
var claimsManager = new Mock<IUserClaimsPrincipalFactory<TestUser>>();
var identityOptions = new IdentityOptions { SecurityStampValidationInterval = TimeSpan.Zero };
identityOptions.ClaimsIdentity.UserIdClaimType = "IdClaim";
var options = new Mock<IOptions<IdentityOptions>>();
options.Setup(a => a.Value).Returns(identityOptions);
var httpContext = new Mock<HttpContext>();
var contextAccessor = new Mock<IHttpContextAccessor>();
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 };