Adrian
Adrian

Reputation: 367

How to Shim System.Security.Principal.WindowsIdentiy in .NET461?

Summary

I have opened a .NET461 solution in VisualStudio 2017 and 2019 and tried to compile it. The build is complaining that the ShimWindowsIdentity cannot be found in the corresponding Test project. There is no problem with the build and especially the ShimWindowsIdentityin Visual Studio 2015.

Goal

I need to shim the constructor of the WindowsIdentity to unit test a function. I know there are possibilities to wrap the WindowsIdentity, but this require code changes (refactoring) I want to avoid for now.

Tried so far

I have tried and checked the following to get the shims for WindowsIdentity working in VS 2017/2019:

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>

Expected

The shims for WindowsIdentiy are generated.

Actual

I see in the mscorlib.4.0.0.0.Fakes.messages the following warning: Cannot generate shim for System.Security.Principal.WindowsIdentity: type is not supported because it is not available or changed between versions.

Upvotes: 2

Views: 405

Answers (1)

amarax
amarax

Reputation: 508

Had some fun time trying to fake HttpRequest.LogonUserIdentity

  1. According to this https://developercommunity.visualstudio.com/content/problem/35053/ms-fakes-is-unable-to-generate-stubs-for-types-inc.html you have to target 4.6.2 or the latest installed .net framework version. Give it a try

  2. I personally was unable to generate shims for this particular class in my environment (with .net 4.8). I tried targeting 461,462,48. With the 4.8 version I end up with another message : Cannot generate shim for System.Security.Principal.WindowsIdentity+d__95: type is not supported because of internal limitations.

For me it doesn't make any sense to target other versions than my web app is built in (4.6.1) so I just gave up and faked HttpRequestBase class with fake wrapper and overrided httpRequest.LogonUserIdenity returning what I need.

Upvotes: 0

Related Questions