From 4a3ad0870c22d6608d64442034afd4f46f40a208 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 17 Apr 2014 12:52:17 -0700 Subject: [PATCH] Port GetUserId/UserName IIdentity extensions --- .../IdentityExtensions.cs | 56 ++++++++++++++ .../IdentityExtensionsTest.cs | 74 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/Microsoft.AspNet.Identity.Security/IdentityExtensions.cs create mode 100644 test/Microsoft.AspNet.Identity.Security.Test/IdentityExtensionsTest.cs diff --git a/src/Microsoft.AspNet.Identity.Security/IdentityExtensions.cs b/src/Microsoft.AspNet.Identity.Security/IdentityExtensions.cs new file mode 100644 index 0000000000..a1b6b400d5 --- /dev/null +++ b/src/Microsoft.AspNet.Identity.Security/IdentityExtensions.cs @@ -0,0 +1,56 @@ +using System.Security.Claims; + +namespace System.Security.Principal +{ + /// + /// Extensions making it easier to get the user name/user id claims off of an identity + /// + public static class IdentityExtensions + { + /// + /// Return the user name using the UserNameClaimType + /// + /// + /// + public static string GetUserName(this IIdentity identity) + { + if (identity == null) + { + throw new ArgumentNullException("identity"); + } + var ci = identity as ClaimsIdentity; + return ci != null ? ci.FindFirstValue(ClaimsIdentity.DefaultNameClaimType) : null; + } + + /// + /// Return the user id using the UserIdClaimType + /// + /// + /// + public static string GetUserId(this IIdentity identity) + { + if (identity == null) + { + throw new ArgumentNullException("identity"); + } + var ci = identity as ClaimsIdentity; + return ci != null ? ci.FindFirstValue(ClaimTypes.NameIdentifier) : null; + } + + /// + /// Return the claim value for the first claim with the specified type if it exists, null otherwise + /// + /// + /// + /// + public static string FindFirstValue(this ClaimsIdentity identity, string claimType) + { + if (identity == null) + { + throw new ArgumentNullException("identity"); + } + var claim = identity.FindFirst(claimType); + return claim != null ? claim.Value : null; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Identity.Security.Test/IdentityExtensionsTest.cs b/test/Microsoft.AspNet.Identity.Security.Test/IdentityExtensionsTest.cs new file mode 100644 index 0000000000..09db26828f --- /dev/null +++ b/test/Microsoft.AspNet.Identity.Security.Test/IdentityExtensionsTest.cs @@ -0,0 +1,74 @@ +using System; +using System.Security.Claims; +using System.Security.Principal; +using Xunit; + +namespace Microsoft.AspNet.Identity.Security.Test +{ + public class IdentityExtensionsTest + { + public const string ExternalAuthenticationType = "TestExternalAuth"; + + [Fact] + public void IdentityNullCheckTest() + { + IIdentity identity = null; + Assert.Throws("identity", () => identity.GetUserId()); + Assert.Throws("identity", () => identity.GetUserName()); + ClaimsIdentity claimsIdentity = null; + Assert.Throws("identity", () => claimsIdentity.FindFirstValue(null)); + } + + [Fact] + public void IdentityNullIfNotClaimsIdentityTest() + { + IIdentity identity = new TestIdentity(); + Assert.Null(identity.GetUserId()); + Assert.Null(identity.GetUserName()); + } + + [Fact] + public void UserNameAndIdTest() + { + var id = CreateTestExternalIdentity(); + Assert.Equal("NameIdentifier", id.GetUserId()); + Assert.Equal("Name", id.GetUserName()); + } + + [Fact] + public void IdentityExtensionsFindFirstValueNullIfUnknownTest() + { + var id = CreateTestExternalIdentity(); + Assert.Null(id.FindFirstValue("bogus")); + } + + private static ClaimsIdentity CreateTestExternalIdentity() + { + return new ClaimsIdentity( + new[] + { + new Claim(ClaimTypes.NameIdentifier, "NameIdentifier", null, ExternalAuthenticationType), + new Claim(ClaimTypes.Name, "Name") + }, + ExternalAuthenticationType); + } + + 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