简体   繁体   中英

Thread.CurrentPrincipal.Identity.Name vs OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name

I have WCF service that authenticate users with windows authentication until yesterday I used OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name to get the current user uses the service but it is hard to do unit tests to it so I switch to Thread.CurrentPrincipal.Identity.Name to retrieve the user that uses the service. My question is if there is any difference between the two in some environment?

Thread.CurrentPrincipal.Identity may not be the authenticated user of your WCF. It could be the identity under which the WCF worker thread is being executed. For example, it could be the IIS user if you host your WCF in IIS.

There are situations where these two values can be different. OperationContext.Current.ServiceSecurityContext.WindowsIdentity is a more reliable source of the callers identity (if its available).

However both OperationContext.Current.ServiceSecurityContext.WindowsIdentity and Thread.CurrentPrincipal.Identity inherit from IIdentity . If your problem is really unit testing:

  1. Change your code dependency to an System.Security.Principal.IIdentity .
  2. When unit testing use Thread.CurrentPrincipal and when running in the WCF service use OperationContext.Current.ServiceSecurityContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM