Port GetUserId/UserName IIdentity extensions

This commit is contained in:
Hao Kung 2014-04-17 12:52:17 -07:00
Родитель 43b780c8ac
Коммит 4a3ad0870c
2 изменённых файлов: 130 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,56 @@
using System.Security.Claims;
namespace System.Security.Principal
{
/// <summary>
/// Extensions making it easier to get the user name/user id claims off of an identity
/// </summary>
public static class IdentityExtensions
{
/// <summary>
/// Return the user name using the UserNameClaimType
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Return the user id using the UserIdClaimType
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Return the claim value for the first claim with the specified type if it exists, null otherwise
/// </summary>
/// <param name="identity"></param>
/// <param name="claimType"></param>
/// <returns></returns>
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;
}
}
}

Просмотреть файл

@ -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<ArgumentNullException>("identity", () => identity.GetUserId());
Assert.Throws<ArgumentNullException>("identity", () => identity.GetUserName());
ClaimsIdentity claimsIdentity = null;
Assert.Throws<ArgumentNullException>("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(); }
}
}
}
}