gpt4 book ai didi

c# - 如何在没有 ControllerContext 的情况下从 IIdentity 模拟 GetUserId 和 IsAuthenticated

转载 作者:行者123 更新时间:2023-11-30 21:48:57 27 4
gpt4 key购买 nike

是否有一种简单的方法来模拟 IIdentity.GetUserId 和 IIdentity.IsAuthenticated?

我已经通过这种方式进行了测试并得到了一个NotSupportedException

[Test]
public void CanGetUserIdFromIdentityTest()
{
//Enviroment
var mockIdentity = new Mock<IIdentity>();
mockIdentity.Setup(x => x.Name).Returns("test@test.com");
mockIdentity.Setup(x => x.IsAuthenticated).Returns(true);
mockIdentity.Setup(x => x.GetUserId()).Returns("12345");

var mockPrincipal = new Mock<IPrincipal>();
mockPrincipal.Setup(x => x.Identity).Returns(mockIdentity.Object);
mockPrincipal.Setup(x => x.IsInRole(It.IsAny<string>())).Returns(true);

//Action
Kernel.Rebind<IPrincipal>().ToConstant(mockPrincipal.Object);

//Asserts
var principal = Kernel.Get<IPrincipal>();
Assert.IsNotNull(principal.Identity.GetUserId());
Assert.IsTrue(principal.Identity.IsAuthenticated);
}

我也使用 GenericIdentity 进行了测试。使用它我可以模拟 GetUserId(),但我不能模拟 IsAuthenticated 属性。

谁能帮帮我?

最佳答案

您得到 NotSupportedException 因为 GetUserId IdentityExtensions.GetUserId Method 的扩展方法 并且不属于模拟对象。无需模拟 GetUserId

如果您查看 GetUserId 的源代码,您会发现它不适合您的地方。

/// <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;
if (ci != null)
{
return ci.FindFirstValue(ClaimsIdentity.DefaultNameClaimType);
}
return 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;
if (ci != null)
{
return ci.FindFirstValue(ClaimTypes.NameIdentifier);
}
return 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;
}
}

它正在寻找具有 ClaimTypes.NameIdentifierClaimsIdentity,这就是它适用于 GenericIdentity 的原因。所以这意味着您需要创建一个身份 stub 。为了使 IsAuthenticated 正常工作,您只需要在构造函数中提供身份验证类型。一个空字符串将起作用。

这是您的测试方法,并进行了更改

[Test]
public void Should_GetUserId_From_Identity() {
//Arrange
var username = "test@test.com";
var identity = new GenericIdentity(username, "");
var nameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, username);
identity.AddClaim(nameIdentifierClaim);

var mockPrincipal = new Mock<IPrincipal>();
mockPrincipal.Setup(x => x.Identity).Returns(identity);
mockPrincipal.Setup(x => x.IsInRole(It.IsAny<string>())).Returns(true);

Kernel.Rebind<IPrincipal>().ToConstant(mockPrincipal.Object);

//Act
var principal = Kernel.Get<IPrincipal>();

//Asserts
Assert.AreEqual(username, principal.Identity.GetUserId());
Assert.IsTrue(principal.Identity.IsAuthenticated);
}

关于c# - 如何在没有 ControllerContext 的情况下从 IIdentity 模拟 GetUserId 和 IsAuthenticated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37264994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com