Setting the windows identity in a unit test
-
Tuesday, November 25, 2008 2:19 PMHi
Could someone point me to an article or advise me how to set the windows identity in my unit tests. I am testing some code that needs to grab the windows principal but when I test it using VSTS the Thread.CurrentPrincipal is already set to a generic principal. I could seem to find any documentation on how to control this so any help appreciated. (I've tried setting the app domain principal policy in code to no effect - I think it has already been set to generic principal by the unit test framework code?).
Thanks
Adrian
http://www.adriancolquhoun.com
All Replies
-
Thursday, November 27, 2008 2:51 AM
Hi Adrian,
Try this
WindowsIdentity.GetCurrent();
You can read here for more information about this class.
Please mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer by Bill.Wang Tuesday, December 02, 2008 1:42 AM
- Unmarked As Answer by Dr Adrian Colquhoun Friday, January 23, 2009 11:36 PM
-
Wednesday, December 31, 2008 9:01 PMWere you ever successful at figuring out how to set the windows identity in the testing framework? I've got the same issue...
Thanks -
Thursday, January 01, 2009 1:49 PMHi
No - never solved it. However, I have some test projects where the tests run quite happily with the current users windows idenity and others (in different solutions) that show the behaviour here. My suspicion is that the problem is caused by having a WCF project in the solution with an anonomous binding but I havent had a chance to test out this theory yet.
Cheers
Adrian
http://www.adriancolquhoun.com -
Sunday, October 25, 2009 5:56 PM
There is a good blog post on this avaialble here:
http://blogs.msdn.com/jimmytr/archive/2007/04/14/writing-test-code-with-impersonation.aspx
Aaron Kowall
http://www.geekswithblogs.net/aaronsblog- Proposed As Answer by Aaron KowallMVP Sunday, October 25, 2009 5:57 PM
-
Monday, February 28, 2011 1:22 PM
This works for me:
var identity = new GenericIdentity(WindowsIdentity.GetCurrent().Name); var principal = new GenericPrincipal(identity, null); Thread.CurrentPrincipal = principal;
Of course the principal isn't of type WindowsIdentity but I didn't need it. The actual code only uses
Thread.CurrentPrincipal.Identity.Name to do a database lookup.Regards,
René
- Proposed As Answer by Jose DG Friday, November 23, 2012 7:24 AM
-
Friday, November 23, 2012 7:24 AM
Thanks a lot!
Worked like a charm!
Regards,
José

