【问题标题】:ClaimTypes.PrimarySid missing from Identity when testing, but only on some environments测试时Identity中缺少ClaimTypes.PrimarySid,但仅在某些环境中
【发布时间】:2017-06-16 17:10:36
【问题描述】:

我有一个已通过所有本地开发人员环境的单元测试,我们的本地构建服务器使用了大约 2 年没有任何问题。

我现在正在将项目转移到另一个构建 - 具有托管和本地构建代理的在线视觉工作室 tfs。仅当通过 TFS 在线运行构建时测试才会失败,并且无论我使用托管还是私有构建代理都会失败。私有构建代理注册为在本地 Windows 帐户下运行的服务。

我可能理解它在托管服务器上失败(也许它没有使用 Windows 登录),但由于它在私有构建代理上也失败了,我有点困惑。

var incomingIdentity = new ClaimsIdentity(Thread.CurrentPrincipal.Identity);

incomingIdentity.AddClaim(
   new Claim(ClaimTypes.NameIdentifier,
   ((ClaimsIdentity)Thread.CurrentPrincipal.Identity)
    .FindAll(ClaimTypes.PrimarySid)
    .Single() //Exception: Sequence contains no elements
    .Value)
);

为什么传入线程标识中缺少ClaimType.PrimarySid

【问题讨论】:

  • 基于我使用 MSTest.exe、VSTest.Console.exe、TFS 和 VSTS 私有构建代理与服务或交互模式的测试(输出所有声明类型),它只包含schemas.xmlsoap.org/ws/2005/05/identity/claims/name。 (在 Visual Studio 中运行测试可以正常运行)如果通过 MSTest 或 VSTest.Console.exe 运行测试,结果如何?

标签: c# .net tfs azure-devops claims-based-identity


【解决方案1】:

在开头添加以下代码,以阐明使用哪个 PrincipalPolicy 可以解决您的问题:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2020-12-04
    相关资源
    最近更新 更多