我将WCF服务配置为使用传输安全性和基本身份验证。
该服务使用vs2010在iiexpress中托管。
我可以从我的客户端代码连接但总是收到:
"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)和之前的博客来设置模块和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来托管服务WCF的替代方案,该服务使用基本的http身份验证,同时需要SSL(https),我会对此感到高兴!
更新 似乎这一直是我的罪魁祸首: Avoid http 401 round trip
然而,我发现我的模块被解雇了(在集成模式下)但是我遇到了一个服务错误,告诉我基本集成是必需的但是没有在主机上启用。
打开iisexpress applicationhost.config文件,果然我找到了:
<section name="basicAuthentication" overrideModeDefault="Deny" />
接着是
<basicAuthentication enabled="false" />
进一步向下
我已将这些更改为<section name="basicAuthentication" overrideModeDefault="Allow" />
并尝试在我的web.config中启用...没有骰子:(