{"id":415,"date":"2010-03-02T23:49:39","date_gmt":"2010-03-02T22:49:39","guid":{"rendered":"http:\/\/www.meadow.se\/wordpress\/?p=415"},"modified":"2011-06-30T10:32:08","modified_gmt":"2011-06-30T09:32:08","slug":"authentication-for-iis-hosted-wcf-services","status":"publish","type":"post","link":"https:\/\/www.meadow.se\/wordpress\/authentication-for-iis-hosted-wcf-services\/","title":{"rendered":"Authentication for IIS-hosted WCF services"},"content":{"rendered":"<p>WCF is very powerful and very, very complicated to configure in many cases. Seemingly simple requirements can get really difficult to get right and security definitely falls in that category. I recently had the following need:<\/p>\n<ul>\n<li>WCF-services hosted in IIS 7.5<\/li>\n<li>Windows authentication should be used for the services<\/li>\n<li>The client must not be required to be in the same domain as the service application<\/li>\n<\/ul>\n<p>Simple enough, right? Not for me it wasn&#8217;t&#8230;<\/p>\n<p>My first idea was to set the authentication settings in IIS to Windows Authentication and configure the client to use credentials from a config file, this was the method I&#8217;m used to when calling ASMX services, but it didn&#8217;t work. It was really frustrating as well as it&#8217;s so difficult to understand what&#8217;s going on. <\/p>\n<p>No matter what changes I did in the configuration files, I kept getting errors such as<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">The HTTP request is unauthorized with client authentication scheme 'Anonymous'.\r\nThe authentication header received from the server was 'Negotiate,NTLM'.<\/pre>\n<p>I was finally able to come up with a solution which I thought I&#8217;d share with you. I don&#8217;t know if it&#8217;s optimal, but if you think it&#8217;s not then please leave a comment.<\/p>\n<p>What I did was this:<\/p>\n<ul>\n<li>Disable IIS authentication by setting it to Anonymous in the IIS Manager<\/li>\n<li>In the binding configuration for the services, set security mode to Message and to use Windows Authentication:\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;security mode=&quot;Message&quot;&gt;\r\n &lt;message clientCredentialType=&quot;Windows&quot; \/&gt;\r\n&lt;\/security&gt;\r\n<\/pre>\n<\/li>\n<li>\nOn the client side we now have the option to pass on the currently logged in user&#8217;s credentials (requires no extra handling, just call the service) or to explicitly give the credentials to use. The latter is done like this for a ClientBase&lt;T&gt; proxy:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclient.ClientCredentials.Windows.ClientCredential.Domain = &quot;xxx&quot;;\r\nclient.ClientCredentials.Windows.ClientCredential.UserName = &quot;yyy&quot;;\r\nclient.ClientCredentials.Windows.ClientCredential.Password = &quot;zzz&quot;;\r\nclient.MyMethod();\r\n<\/pre>\n<\/li>\n<p>This solved the problem for me. I think the major difficulty I had was that IIS and WCF security settings collided, so I believe the lesson learned is to do security on only one place and that place should probably be WCF.<\/p>\n<p>On a related note, I have also had problems with service discovery, WSDL, etc if the web application is not set to Anonymous authentication in IIS, so it seems to be a good idea to do it like that for that reason as well.<\/p>\n<p>Also, I&#8217;m not the only one to have problems with WCF security, here are a few others:<br \/>\n<a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/wcf\/thread\/838a6572-a733-463f-97f5-eb127a93f070\">Post 1<\/a>, <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/wcf\/thread\/ab7c0abf-141f-48d5-b5ba-2a3129539019\">Post 2<\/a>.<\/p>\n<p>\/Emil<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>WCF is very powerful and very, very complicated to configure in many cases. Seemingly simple requirements can get really difficult to get right and security definitely falls in that category. I recently had the following need: WCF-services hosted in IIS 7.5 Windows authentication should be used for the services The client must not be required <a class=\"more-link\" href=\"https:\/\/www.meadow.se\/wordpress\/authentication-for-iis-hosted-wcf-services\/\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[25],"_links":{"self":[{"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/posts\/415"}],"collection":[{"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":5,"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":584,"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/posts\/415\/revisions\/584"}],"wp:attachment":[{"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.meadow.se\/wordpress\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}