Move extensions IPrincipal => ClaimsPrincipal
Fixes https://github.com/aspnet/Identity/issues/395
This commit is contained in:
parent
268af34244
commit
a6bf4029aa
|
|
@ -1,5 +1,5 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Principal;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using IdentitySample.Models;
|
using IdentitySample.Models;
|
||||||
using IdentitySamples;
|
using IdentitySamples;
|
||||||
|
|
|
||||||
|
|
@ -2,62 +2,58 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
|
|
||||||
namespace System.Security.Principal
|
namespace System.Security.Claims
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Claims related extensions for <see cref="IPrincipal"/>.
|
/// Claims related extensions for <see cref="ClaimsPrincipal"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class PrincipalExtensions
|
public static class PrincipalExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the Name claim value if present otherwise returns null.
|
/// Returns the Name claim value if present otherwise returns null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
/// <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>
|
/// <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 IPrincipal principal)
|
public static string GetUserName(this ClaimsPrincipal principal)
|
||||||
{
|
{
|
||||||
if (principal == null)
|
if (principal == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(principal));
|
throw new ArgumentNullException(nameof(principal));
|
||||||
}
|
}
|
||||||
var cp = principal as ClaimsPrincipal;
|
return principal.FindFirstValue(ClaimsIdentity.DefaultNameClaimType);
|
||||||
return cp != null ? cp.FindFirstValue(ClaimsIdentity.DefaultNameClaimType) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the User ID claim value if present otherwise returns null.
|
/// Returns the User ID claim value if present otherwise returns null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
/// <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>
|
/// <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 IPrincipal principal)
|
public static string GetUserId(this ClaimsPrincipal principal)
|
||||||
{
|
{
|
||||||
if (principal == null)
|
if (principal == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(principal));
|
throw new ArgumentNullException(nameof(principal));
|
||||||
}
|
}
|
||||||
var ci = principal as ClaimsPrincipal;
|
return principal.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
return ci != null ? ci.FindFirstValue(ClaimTypes.NameIdentifier) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if the principal has an identity with the application cookie identity
|
/// Returns true if the principal has an identity with the application cookie identity
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="principal">The <see cref="IPrincipal"/> instance this method extends.</param>
|
/// <param name="principal">The <see cref="ClaimsPrincipal"/> 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 IPrincipal principal)
|
public static bool IsSignedIn(this ClaimsPrincipal principal)
|
||||||
{
|
{
|
||||||
if (principal == null)
|
if (principal == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(principal));
|
throw new ArgumentNullException(nameof(principal));
|
||||||
}
|
}
|
||||||
var p = principal as ClaimsPrincipal;
|
return principal?.Identities != null &&
|
||||||
return p?.Identities != null &&
|
principal.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType);
|
||||||
p.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Security.Principal;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Authentication.Cookies;
|
using Microsoft.AspNet.Authentication.Cookies;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Security.Principal;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Identity.Test
|
namespace Microsoft.AspNet.Identity.Test
|
||||||
|
|
@ -15,19 +14,10 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
[Fact]
|
[Fact]
|
||||||
public void IdentityNullCheckTest()
|
public void IdentityNullCheckTest()
|
||||||
{
|
{
|
||||||
IPrincipal p = null;
|
ClaimsPrincipal p = null;
|
||||||
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserId());
|
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserId());
|
||||||
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserName());
|
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserName());
|
||||||
ClaimsPrincipal cp = null;
|
Assert.Throws<ArgumentNullException>("principal", () => p.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]
|
||||||
|
|
@ -82,39 +72,5 @@ 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