Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Windows Username from WCF server side

Tags:

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.

like image 901
Ana Betts Avatar asked Nov 15 '08 06:11

Ana Betts


People also ask

How do I bypass WCF username and password?

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.

What is a WCF 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.


3 Answers

Try looking at ServiceSecurityContext.Current.WindowsIdentity

like image 134
Joe Avatar answered Nov 10 '22 22:11

Joe


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();

}
like image 35
Mitch Baker Avatar answered Nov 10 '22 23:11

Mitch Baker


To get the WCF Service caller username:

var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;

like image 36
Francisco Goldenstein Avatar answered Nov 11 '22 00:11

Francisco Goldenstein