Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / security

Sending Spn in secured WCF Service

4.00/5 (1 vote)
4 Feb 2010CPOL 14.9K  
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)