Rollback moving IPrincipal extensions
This commit is contained in:
parent
2246a41978
commit
9175e6366c
|
|
@ -18,13 +18,14 @@ namespace System.Security.Principal
|
||||||
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
||||||
/// <returns>The Name claim value, or null if the claim is not present.</returns>
|
/// <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>
|
/// <remarks>The name claim is identified by <see cref="ClaimsIdentity.DefaultNameClaimType"/>.</remarks>
|
||||||
public static string GetUserName(this ClaimsPrincipal principal)
|
public static string GetUserName(this IPrincipal principal)
|
||||||
{
|
{
|
||||||
if (principal == null)
|
if (principal == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(principal));
|
throw new ArgumentNullException(nameof(principal));
|
||||||
}
|
}
|
||||||
return principal.FindFirstValue(ClaimsIdentity.DefaultNameClaimType);
|
var cp = principal as ClaimsPrincipal;
|
||||||
|
return cp != null ? cp.FindFirstValue(ClaimsIdentity.DefaultNameClaimType) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -33,13 +34,14 @@ namespace System.Security.Principal
|
||||||
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
||||||
/// <returns>The User ID claim value, or null if the claim is not present.</returns>
|
/// <returns>The User ID claim value, or null if the claim is not present.</returns>
|
||||||
/// <remarks>The name claim is identified by <see cref="ClaimTypes.NameIdentifier"/>.</remarks>
|
/// <remarks>The name claim is identified by <see cref="ClaimTypes.NameIdentifier"/>.</remarks>
|
||||||
public static string GetUserId(this ClaimsPrincipal principal)
|
public static string GetUserId(this IPrincipal principal)
|
||||||
{
|
{
|
||||||
if (principal == null)
|
if (principal == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(principal));
|
throw new ArgumentNullException(nameof(principal));
|
||||||
}
|
}
|
||||||
return principal.FindFirstValue(ClaimTypes.NameIdentifier);
|
var ci = principal as ClaimsPrincipal;
|
||||||
|
return ci != null ? ci.FindFirstValue(ClaimTypes.NameIdentifier) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -47,14 +49,15 @@ namespace System.Security.Principal
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
||||||
/// <returns>True if the user is logged in with identity.</returns>
|
/// <returns>True if the user is logged in with identity.</returns>
|
||||||
public static bool IsSignedIn(this ClaimsPrincipal principal)
|
public static bool IsSignedIn(this IPrincipal principal)
|
||||||
{
|
{
|
||||||
if (principal == null)
|
if (principal == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(principal));
|
throw new ArgumentNullException(nameof(principal));
|
||||||
}
|
}
|
||||||
return principal.Identities != null &&
|
var p = principal as ClaimsPrincipal;
|
||||||
principal.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType);
|
return p?.Identities != null &&
|
||||||
|
p.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,21 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
[Fact]
|
[Fact]
|
||||||
public void IdentityNullCheckTest()
|
public void IdentityNullCheckTest()
|
||||||
{
|
{
|
||||||
|
IPrincipal p = null;
|
||||||
|
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserId());
|
||||||
|
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserName());
|
||||||
ClaimsPrincipal cp = null;
|
ClaimsPrincipal cp = null;
|
||||||
Assert.Throws<ArgumentNullException>("principal", () => cp.GetUserId());
|
|
||||||
Assert.Throws<ArgumentNullException>("principal", () => cp.GetUserName());
|
|
||||||
Assert.Throws<ArgumentNullException>("principal", () => cp.FindFirstValue(null));
|
Assert.Throws<ArgumentNullException>("principal", () => cp.FindFirstValue(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void IdentityNullIfNotClaimsIdentityTest()
|
||||||
|
{
|
||||||
|
IPrincipal identity = new TestPrincipal();
|
||||||
|
Assert.Null(identity.GetUserId());
|
||||||
|
Assert.Null(identity.GetUserName());
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void UserNameAndIdTest()
|
public void UserNameAndIdTest()
|
||||||
{
|
{
|
||||||
|
|
@ -73,5 +82,39 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
},
|
},
|
||||||
ExternalAuthenticationScheme));
|
ExternalAuthenticationScheme));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class TestPrincipal : IPrincipal
|
||||||
|
{
|
||||||
|
public IIdentity Identity
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsInRole(string role)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestIdentity : IIdentity
|
||||||
|
{
|
||||||
|
public string AuthenticationType
|
||||||
|
{
|
||||||
|
get { throw new NotImplementedException(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsAuthenticated
|
||||||
|
{
|
||||||
|
get { throw new NotImplementedException(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get { throw new NotImplementedException(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue