Markus
Markus

Reputation: 22466

WCF Custom Authentication: Impersonate Windows Account for duration of request

for a customer, I need to implement the following scenario:

What I am looking for is a way to implement the impersonation in one place instead of each service method separately.

How can I implement this in the service?

Thanks for your help,

Markus

Upvotes: 0

Views: 1102

Answers (2)

Stix
Stix

Reputation: 495

Actually this link gives the answer: ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations

A Snippet:

For details, including how impersonation is performed when using Allowed together with the ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations property, see Delegation and Impersonation with WCF and How to: Impersonate a Client on a Service.

Below is the literal link.

https://msdn.microsoft.com/en-us/library/system.servicemodel.operationbehaviorattribute.impersonation(v=vs.110).aspx

Upvotes: 0

Vincent Vancalbergh
Vincent Vancalbergh

Reputation: 3337

Perhaps this example can get you on the way: taken from here

public class HelloService : IHelloService
{
    [OperationBehavior(Impersonation = ImpersonationOption.Required)]
    public string Hello(string message)
    {
        WindowsIdentity callerWindowsIdentity = ServiceSecurityContext.Current.WindowsIdentity;
        if (callerWindowsIdentity == null)
        {
            throw new InvalidOperationException
             ("The caller cannot be mapped to a Windows identity.");
        }
        using (callerWindowsIdentity.Impersonate())
        {
            EndpointAddress backendServiceAddress = new EndpointAddress("http://localhost:8000/ChannelApp");
            // Any binding that performs Windows authentication of the client can be used.
            ChannelFactory<IHelloService> channelFactory = new ChannelFactory<IHelloService>(new NetTcpBinding(), backendServiceAddress);
            IHelloService channel = channelFactory.CreateChannel();
            return channel.Hello(message);
        }
    }
}

Upvotes: 1

Related Questions