2011-04-07 111 views
3

我有一個WCF服務配置爲使用傳輸安全性和基本身份驗證。使用iisexpress傳輸安全性和基本身份驗證的WCF服務身份驗證總是返回401

該服務在iiexpress與vs2010中進行託管。

我能夠從我的客戶端代碼連接,但總是收到:

"The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm=realm'." 

這有一個內部異常:

"The remote server returned an error: (401) Unauthorized." 

類似Can not call web service with basic authentication using WCF雖然我的客戶端代碼已經設置在答案中列出。

我也跟着HTTP Basic Authentication against Non-Windows Accounts in IIS/ASP.NET (Part 3 - Adding WCF Support)和以前的博客建立了一個Module和IAuthorizationPolicy類。

IISExpress配置爲經典模式,禁用匿名和Windows身份驗證並啓用SSL。

客戶端配置:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="NotificationHttpBinding"> 
      <security mode="Transport"> 
      <transport clientCredentialType="Basic" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://localhost/NotificationService.svc" 
     binding="basicHttpBinding" bindingConfiguration="NotificationHttpBinding" 
     contract="NotificationPortType" name="BasicHttpBinding_NotificationPortType" /> 
    </client> 
    </system.serviceModel> 

服務配置:

<system.serviceModel> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 

    <services> 
     <service name="Notification.NotificationService" behaviorConfiguration="NotificationServiceBehavior"> 
     <endpoint binding="basicHttpBinding" contract="NotificationPortType" bindingConfiguration="NotificationHttpBinding" > 
     </endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="NotificationServiceBehavior"> 
      <serviceMetadata /> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
      <serviceAuthorization> 
      <authorizationPolicies> 
       <add policyType="Notification.HttpContextIdentityPolicy, Notification" /> 
      </authorizationPolicies> 
      </serviceAuthorization> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <bindings> 
     <basicHttpBinding> 
     <binding name="NotificationHttpBinding"> 
      <security mode="Transport"> 
      <transport clientCredentialType="Basic" /> 
      </security> 

     </binding> 
     </basicHttpBinding> 
    </bindings> 

    </system.serviceModel> 


    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

    <system.web> 

    <httpModules> 
     <add name="CustomBasicAuthentication" type="Notification.CustomBasicAuthenticationModule, Notification"/> 
    </httpModules> 

    <membership defaultProvider="SampleProvider"> 
     <providers> 
     <add name="SampleProvider" type="Notification.HardcodedSecurityProviders, Notification" /> 
     </providers> 
    </membership> 

    </system.web> 

客戶端代碼沒什麼大事:

static void Main(string[] args) 
     { 
      ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { return true; }; 

      NotificationPortTypeClient client = new NotificationPortTypeClient("BasicHttpBinding_NotificationPortType"); 

      client.ClientCredentials.UserName.UserName = "Test"; 
      client.ClientCredentials.UserName.Password = "PWD"; 


      client.sendNotification(new NotificationRequest()); 
     } 

或者 如果有人能告訴我如何替代使用IIS6來st服務WCF使用基本http認證,同時要求SSL(https)我會很高興!


UPDATE 看來,這是我的罪魁禍首一直: Avoid http 401 round trip

然而,我發現我的模塊發射罰款(集成模式),但我然後用服務錯誤告訴我介紹基本集成是必需的,但在主機上未啓用。

開闢iisexpress ApplicationHost.config文件果然,我發現:依次

<basicAuthentication enabled="false" /> 

進一步下跌

我已經改變了這些以<section name="basicAuthentication" overrideModeDefault="Allow" />

<section name="basicAuthentication" overrideModeDefault="Deny" /> 

並試圖啓用我的web.config ...沒有骰子:(

+0

不是一個直接的答案,但MS現在已經發布了IIS7 [版本](http://www.microsoft.com/downloads/en/details.aspx?FamilyID=abc59783-89de-4adc-b770-0a720bb21deb)專門用於開發站。這個版本比VS自帶的小型服務器更好,因爲它更接近應用程序最終運行的版本。也許如果你設置它,根據需要配置會更容易。 [有用的文章](http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx) – Menahem 2011-04-07 12:44:39

+0

是的,IISExpress 7.5與VS2010 SP1集成功能。我已經在使用它了。 – MattC 2011-04-07 12:50:00

+0

在你提供的鏈接(3部分事物)中注意到傳輸憑證被設置爲None,並且解釋說WCF沒有執行auth,但IIS httpmodule是。你可以試一試嗎? – Menahem 2011-04-07 13:07:10

回答

-2

您需要使用WSHttpBinding

有一個完整的樣本here

+0

不幸的是,我要求非WCF平臺能夠使用SOAP 1.1進行訪問,以便basicHttpBinding。 – MattC 2011-04-07 12:51:41