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.
Ho开发者_高级运维w 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.
e.g.
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.
精彩评论