From a6bf4029aa24a4ae3ce28924d0af02a56227d159 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 19 Mar 2015 15:24:20 -0700 Subject: [PATCH] Move extensions IPrincipal => ClaimsPrincipal Fixes https://github.com/aspnet/Identity/issues/395 --- .../Controllers/ManageController.cs | 2 +- .../PrincipalExtensions.cs | 28 +++++------ .../SecurityStampValidator.cs | 2 +- .../PrincipalExtensionsTest.cs | 48 +------------------ 4 files changed, 16 insertions(+), 64 deletions(-) diff --git a/samples/IdentitySample.Mvc/Controllers/ManageController.cs b/samples/IdentitySample.Mvc/Controllers/ManageController.cs index 13223ceed9..f33dfe020a 100644 --- a/samples/IdentitySample.Mvc/Controllers/ManageController.cs +++ b/samples/IdentitySample.Mvc/Controllers/ManageController.cs @@ -1,5 +1,5 @@ using System.Linq; -using System.Security.Principal; +using System.Security.Claims; using System.Threading.Tasks; using IdentitySample.Models; using IdentitySamples; diff --git a/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs b/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs index 669af4cfa3..6aaf667d4c 100644 --- a/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs +++ b/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs @@ -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 { /// - /// Claims related extensions for . + /// Claims related extensions for . /// public static class PrincipalExtensions { /// /// Returns the Name claim value if present otherwise returns null. /// - /// The instance this method extends. + /// 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 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); } /// /// Returns the User ID claim value if present otherwise returns null. /// - /// The instance this method extends. + /// The instance this method extends. /// The User ID claim value, or null if the claim is not present. /// The name claim is identified by . - 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); } /// /// Returns true if the principal has an identity with the application cookie identity /// - /// The instance this method extends. + /// The instance this method extends. /// True if the user is logged in with identity. - 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); } /// diff --git a/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs b/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs index f8ba4b9e0b..12e50cf227 100644 --- a/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs +++ b/src/Microsoft.AspNet.Identity/SecurityStampValidator.cs @@ -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; diff --git a/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs b/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs index 8a2e5b3a47..ce56993d13 100644 --- a/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs @@ -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("principal", () => p.GetUserId()); Assert.Throws("principal", () => p.GetUserName()); - ClaimsPrincipal cp = null; - Assert.Throws("principal", () => cp.FindFirstValue(null)); - } - - [Fact] - public void IdentityNullIfNotClaimsIdentityTest() - { - IPrincipal identity = new TestPrincipal(); - Assert.Null(identity.GetUserId()); - Assert.Null(identity.GetUserName()); + Assert.Throws("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(); } - } - } } } \ No newline at end of file