gpt4 book ai didi

c# - 如何在 .NET461 中填充 System.Security.Principal.WindowsIdentiy?

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:25 25 4
gpt4 key购买 nike

总结

我在VisualStudio 2017和2019中打开了一个.NET461解决方案并尝试编译它。该构建提示在相应的测试项目中找不到 ShimWindowsIdentity。构建没有问题,尤其是 Visual Studio 2015 中的 ShimWindowsIdentity

目标

我需要填充 WindowsIdentity 的构造函数以对函数进行单元测试。我知道有可能包装 WindowsIdentity,但这需要我现在想避免的代码更改(重构)。

到目前为止已经尝试过

我已经尝试并检查了以下内容以获得在 VS 2017/2019 中工作的 WindowsIdentity 的垫片:

  • 删除假程序集并从头开始构建它们
  • 禁用 stub 生成并显式添加 mscorlib.fakes 所需的 stub 和垫片
  • 查看项目和Test项目中的TargetFrameworkVersion v4.6.1

mscorlib.fakes

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="mscorlib" Version="4.0.0.0"/>
<StubGeneration>
<Clear />
<Add FullName="System.IO.Stream!"/>
</StubGeneration>
<ShimGeneration>
<Clear/>
<Add FullName="System.Security.Principal"/>
<Add FullName="System.DateTime!"/>
<Add FullName="System.IO.Stream!"/>
</ShimGeneration>
</Fakes>

预期

WindowsIdentiy 的 shim 已生成。

实际

我在 mscorlib.4.0.0.0.Fakes.messages 中看到以下警告:Cannot generate shim for System.Security.Principal.WindowsIdentity: type is not supported because it is not版本之间可用或更改。

最佳答案

尝试伪造 HttpRequest.LogonUserIdentity 玩得很开心

  1. 据此 https://developercommunity.visualstudio.com/content/problem/35053/ms-fakes-is-unable-to-generate-stubs-for-types-inc.html您必须以 4.6.2 或最新安装的 .net 框架版本为目标。试一试

  2. 我个人无法在我的环境(使用 .net 4.8)中为这个特定类生成垫片。我试着瞄准 461,462,48。在 4.8 版本中,我得到了另一条消息:无法为 System.Security.Principal.WindowsIdentity+d__95 生成填充码:由于内部限制,不支持类型。

对我来说,除了我的 web 应用程序是内置的 (4.6.1) 之外,定位其他版本没有任何意义,所以我只是放弃并使用假包装器伪造 HttpRequestBase 类并覆盖 httpRequest.LogonUserIdenity 返回我需要的东西。

关于c# - 如何在 .NET461 中填充 System.Security.Principal.WindowsIdentiy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55627066/

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