Move extensions IPrincipal => ClaimsPrincipal

Fixes https://github.com/aspnet/Identity/issues/395
This commit is contained in:
Hao Kung 2015-03-19 15:24:20 -07:00
parent 268af34244
commit a6bf4029aa
4 changed files with 16 additions and 64 deletions

View File

@ -1,5 +1,5 @@
using System.Linq;
using System.Security.Principal;
using System.Security.Claims;
using System.Threading.Tasks;
using IdentitySample.Models;
using IdentitySamples;

View File

@ -2,62 +2,58 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNet.Identity;
namespace System.Security.Principal
namespace System.Security.Claims
{
/// <summary>
/// Claims related extensions for <see cref="IPrincipal"/>.
/// Claims related extensions for <see cref="ClaimsPrincipal"/>.
/// </summary>
public static class PrincipalExtensions
{
/// <summary>
/// Returns the Name claim value if present otherwise returns null.
/// </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>
/// <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)
{
throw new ArgumentNullException(nameof(principal));
}
var cp = principal as ClaimsPrincipal;
return cp != null ? cp.FindFirstValue(ClaimsIdentity.DefaultNameClaimType) : null;
return principal.FindFirstValue(ClaimsIdentity.DefaultNameClaimType);
}
/// <summary>
/// Returns the User ID claim value if present otherwise returns null.
/// </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>
/// <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)
{
throw new ArgumentNullException(nameof(principal));
}
var ci = principal as ClaimsPrincipal;
return ci != null ? ci.FindFirstValue(ClaimTypes.NameIdentifier) : null;
return principal.FindFirstValue(ClaimTypes.NameIdentifier);
}
/// <summary>
/// Returns true if the principal has an identity with the application cookie identity
/// </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>
public static bool IsSignedIn(this IPrincipal principal)
public static bool IsSignedIn(this ClaimsPrincipal principal)
{
if (principal == null)
{
throw new ArgumentNullException(nameof(principal));
}
var p = principal as ClaimsPrincipal;
return p?.Identities != null &&
p.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType);
return principal?.Identities != null &&
principal.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType);
}
/// <summary>

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Principal;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.Framework.DependencyInjection;

View File

@ -3,7 +3,6 @@
using System;
using System.Security.Claims;
using System.Security.Principal;
using Xunit;
namespace Microsoft.AspNet.Identity.Test
@ -15,19 +14,10 @@ namespace Microsoft.AspNet.Identity.Test
[Fact]
public void IdentityNullCheckTest()
{
IPrincipal p = null;
ClaimsPrincipal p = null;
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserId());
Assert.Throws<ArgumentNullException>("principal", () => p.GetUserName());
ClaimsPrincipal cp = null;
Assert.Throws<ArgumentNullException>("principal", () => cp.FindFirstValue(null));
}
[Fact]
public void IdentityNullIfNotClaimsIdentityTest()
{
IPrincipal identity = new TestPrincipal();
Assert.Null(identity.GetUserId());
Assert.Null(identity.GetUserName());
Assert.Throws<ArgumentNullException>("principal", () => p.FindFirstValue(null));
}
[Fact]
@ -82,39 +72,5 @@ namespace Microsoft.AspNet.Identity.Test
},
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(); }
}
}
}
}