简体   繁体   中英

Find a WCF service caller's Active Directory domain username

Consider a WCF service using WsHttpBinding for which only the domain users are allowed to call this service.

How can you find the Active Directory username of the caller?

Get the value of System.ServiceModel.ServiceSecurityContext.Current.WindowsIdentity.Name property.

It does not matter which binding you use as long as the security mode is different from None for the binding.

If the security mode is None then System.ServiceModel.ServiceSecurityContext.Current will be null .

You can get identity of the user by calling:

ServiceSecurityContext.Current.WindowsIdentity.Name

or

OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name

You will have to add some sort of User information to the message structure you are using to contact the service.

eg

public class UserInformation
{
  public string User { get; set; }
  public string Password { get; set; }
}

[DataContract]
public class Request
{
  [DataMember]
  public UserInformation User { get; set; }
  [DataMember]
  public MyRequest RequestBody { get; set; }
}

This way you can query active directory at your client side, populate the UserInformation object and send over the user details as part of the message structure.

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