65.9K
CodeProject is changing. Read more.
Home

Sending Spn in secured WCF Service

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Feb 3, 2010

CPOL
viewsIcon

14873

You have a WCF Service that needs a SPN from its client But We typically use client config file to send the spn using the below code: <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttp" name="BasicEndPoint" bindingNamespace="TestNamespace" ...

You have a WCF Service that needs a SPN from its client But We typically use client config file to send the spn using the below code:
 <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttp"
				 name="BasicEndPoint" bindingNamespace="TestNamespace"
				 contract="IService">
          <identity>
            <servicePrincipalName value="HTTP/TEST"/>
          </identity>
        </endpoint>
But in case of .net vetsion 3.5 SP1 i is not working at all because the WCF client is not sent the spn value from config files. So it was not work at all Use the below code to force fully send the SPN name to a WCF Service from a WCF Client:
IService proxy = null;
ChannelFactory<IService> factory = null;
EndpointIdentity identity = null;
EndpointAddress address = null;
if (!string.IsNullOrEmpty(spnName))
    identity = EndpointIdentity.CreateSpnIdentity(spnName);//  < --- HARD CODE THE SPN VALUE HERE (HTTP/TEST)
if (identity != null)
    address = new EndpointAddress(new Uri(webServiceUrl), identity);  //< ----- PROVIDE THE SERVICE URL HERE
else
    address = new EndpointAddress(new Uri(webServiceUrl));  
factory = new ChannelFactory<IService>(bindingName, address);
factory.Credentials.Windows.AllowNtlm = true;
factory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
proxy = factory.CreateChannel();
use this Proxy Object you can call operations of the service and also send SPN Properly