I'm pretty green with web services and WCF, and I'm using Windows integrated authentication - how do I get the username on the server-side interface? I believe that I'm supposed to implement a custom Behavior, or perhaps something with WCF Sessions? Any clues would be super-handy.
To configure a service to authenticate its clients using Windows Domain username and passwords use the WSHttpBinding and set its Security. Mode property to Message . In addition you must specify an X509 certificate that will be used to encrypt the username and password as they are sent from the client to the service.
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.
Try looking at ServiceSecurityContext.Current.WindowsIdentity
Here is a snippet of service code that shows how you could retrieve and use the WindowsIdentity associated with the caller of a WCF service.
This code is assuming that you are accepting most of the defaults with your configuration. It should work without any problems with the Named Pipe or the Net TCP binding.
the p.Demand() will determine if the user is in the windows group specified by the permissionGroup variable.
private static void DemandManagerPermission()
{
// Verify the use has authority to proceed
string permissionGroup = ConfigurationManager.AppSettings["ManagerPermissionGroup"];
if (string.IsNullOrEmpty(permissionGroup))
throw new FaultException("Group permissions not set for access control.");
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
var p = new PrincipalPermission(ServiceSecurityContext.Current.WindowsIdentity.Name, permissionGroup, true);
p.Demand();
}
To get the WCF Service caller username:
var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With