一篇讲解如何实现WCF Impersonate的文章,讲的通俗易懂。
原文地址:
原文内容:
====================
When we started WCFing in my current project, the first challenge that we had was to get the WCF Services to impersonate the callers, I had burned a lot of mid night oil trying to figure out the right configuration that would make this happen. Till that time I had not gone through the WCF Security Guidance on CodePlex. The Guidance turned out to be quite helpful and very comprehensive. If you have not gone though that yet, I strongly recommend going to CodePlex and reading it right away.
For those who do not have that much time and do not want to go too much into the theoretical side of side of it, I have put together a small how to on setting up WCF Services to Impersonate Client credentials. Please follow these simple steps and you will be good to go.
WCF Side
1. Configure your service to use Windows Authentication and Message Security.
<wsHttpBinding>
<binding name="WSHttpBinding_IService”>
<security mode="Message">
<transport clientCredentialType="Windows"
proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows"
negotiateServiceCredential="true"
algorithmSuite="Default"
establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
2. Configure the SPN Identity for the WCF Service Endpoint
Configure the service principle name (SPN) identity under which the WCF service will run, this identity is usually the lower-privilege Network Service account. Use of this account will reduce the attack surface when your application is not impersonating.
Now the configuration should look like this.
<service behaviorConfiguration="ServiceBehavior" name="ServiceSI">
<endpoint address="" binding="wsHttpBinding" contract="IService">
<identity>
<servicePrincipalName value="A2PD-MANESHK" />
<dns value="" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
3. Set up impersonation for all operations
Perform the following steps to impersonate all operations:
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceAuthorization impersonateCallerForAllOperations="true" />
</behavior>
4. Implement Impersonation in the WCF Service
Perform the following steps to declaratively impersonate specific operations:
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public ServiceBE CreateService(ServiceBE serviceBE)
{
//Code
}
Note: When impersonating for all operations, the Impersonation property of the OperationBehaviorAttribute applied to each method must also be set to either Allowed or Required.
5. Impersonating the Original Caller Programmatically.
Perform the following step to impersonate the original caller programmatically:
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public ServiceBE CreateService(ServiceBE serviceBE)
{
if ((ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Impersonation)
|| (ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Delegation))
{
using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
{
//Your code goes in here
}
}
}
Client Side
1. Create the Client Configuration file from the WCF Config
Perform the following steps for creating a client configuration file from the newly created wcf configuration file.