diff --git a/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs b/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs index c01cb0778c..669af4cfa3 100644 --- a/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs +++ b/src/Microsoft.AspNet.Identity/PrincipalExtensions.cs @@ -18,13 +18,14 @@ namespace System.Security.Principal /// 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 ClaimsPrincipal principal) + public static string GetUserName(this IPrincipal principal) { if (principal == null) { throw new ArgumentNullException(nameof(principal)); } - return principal.FindFirstValue(ClaimsIdentity.DefaultNameClaimType); + var cp = principal as ClaimsPrincipal; + return cp != null ? cp.FindFirstValue(ClaimsIdentity.DefaultNameClaimType) : null; } /// @@ -33,13 +34,14 @@ namespace System.Security.Principal /// 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 ClaimsPrincipal principal) + public static string GetUserId(this IPrincipal principal) { if (principal == null) { throw new ArgumentNullException(nameof(principal)); } - return principal.FindFirstValue(ClaimTypes.NameIdentifier); + var ci = principal as ClaimsPrincipal; + return ci != null ? ci.FindFirstValue(ClaimTypes.NameIdentifier) : null; } /// @@ -47,14 +49,15 @@ namespace System.Security.Principal /// /// The instance this method extends. /// True if the user is logged in with identity. - public static bool IsSignedIn(this ClaimsPrincipal principal) + public static bool IsSignedIn(this IPrincipal principal) { if (principal == null) { throw new ArgumentNullException(nameof(principal)); } - return principal.Identities != null && - principal.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType); + var p = principal as ClaimsPrincipal; + return p?.Identities != null && + p.Identities.Any(i => i.AuthenticationType == IdentityOptions.ApplicationCookieAuthenticationType); } /// diff --git a/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs b/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs index 6d63e70e49..8a2e5b3a47 100644 --- a/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs +++ b/test/Microsoft.AspNet.Identity.Test/PrincipalExtensionsTest.cs @@ -15,12 +15,21 @@ namespace Microsoft.AspNet.Identity.Test [Fact] public void IdentityNullCheckTest() { + IPrincipal p = null; + Assert.Throws("principal", () => p.GetUserId()); + Assert.Throws("principal", () => p.GetUserName()); ClaimsPrincipal cp = null; - Assert.Throws("principal", () => cp.GetUserId()); - Assert.Throws("principal", () => cp.GetUserName()); Assert.Throws("principal", () => cp.FindFirstValue(null)); } + [Fact] + public void IdentityNullIfNotClaimsIdentityTest() + { + IPrincipal identity = new TestPrincipal(); + Assert.Null(identity.GetUserId()); + Assert.Null(identity.GetUserName()); + } + [Fact] public void UserNameAndIdTest() { @@ -73,5 +82,39 @@ 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