Wcf基本身份验证
在简单的测试Wcf服务中使用基本身份验证遇到一些麻烦。我收到一个例外:Wcf基本身份验证
无法激活请求的服务'http://qld-tgower/test/Service.svc'。请参阅>服务器的诊断跟踪日志以获取更多信息。
而在跟踪日志它显示:
主机(“基本”)上配置的身份验证方案不允许那些绑定“basicHttpBinding的”(“匿名”)进行配置。请确保SecurityMode设置为Transport或TransportCredentialOnly。此外,可以通过更改此应用程序的身份验证方案,通过IIS管理工具,通过ServiceHost.Authentication.AuthenticationSchemes属性,<serviceAuthenticationManager>元素上的应用程序配置文件,通过更新绑定上的ClientCredentialType属性,或者通过调整HttpTransportBindingElement上的AuthenticationScheme属性。
但当我我们不正确的用户名和密码,它使用基本身份验证说它IS什么我不明白呢?
该HTTP请求未经授权,客户端身份验证方案为“基本”。从服务器收到的验证头是'Basic realm =“qld-tgower”'。
这是我的web.config细节
<system.serviceModel>
<services>
<service name="WcfService"
behaviorConfiguration="Behavior">
<endpoint address="http://QLD-TGOWER/test/Service.svc"
binding="basicHttpBinding"
bindingConfiguration="httpBinding"
contract="IService" />
</service>
</services>
<diagnostics>
<endToEndTracing activityTracing="false" messageFlowTracing="true" propagateActivity="true"></endToEndTracing>
</diagnostics>
<bindings>
<basicHttpBinding>
<binding name="httpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" proxyCredentialType="Basic">
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
,这是我的App.config
<system.serviceModel>
<diagnostics>
<endToEndTracing activityTracing="true" />
<messageLogging logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" >
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"></transport>
<message clientCredentialType="UserName" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://QLD-TGOWER/test/Service.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>
我的测试应用程序
private static void Main(string[] args)
{
var proxy = new ServiceClient("BasicHttpBinding_IService");
var clientCredentials = proxy.ClientCredentials;
clientCredentials.UserName.UserName = "username";
clientCredentials.UserName.Password = "password";
var res = proxy.GetData(1);
Console.WriteLine(res);
Console.WriteLine("Done");
Console.ReadKey(true);
}
我的服务
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
有没有,我很想念这里的东西吗?
更改服务的名称和合约以包含名称空间。
另外,删除端点地址(将其设置为“”),并且不要在transport标签中包含proxyCredentialType。
web.config中的最终结果应该是这个样子
<system.serviceModel>
<services>
<service name="MyNameSpace.MyService" behaviorConfiguration="asdf">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="httpBinding" contract="MyNameSpace.IMyService" />
</service>
</services>
<diagnostics>
<endToEndTracing activityTracing="true" messageFlowTracing="true"
propagateActivity="true">
</endToEndTracing>
</diagnostics>
<bindings>
<basicHttpBinding>
<binding name="httpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="asdf">
<!-- To avoid disclosing metadata information, set the value below to
false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment to avoid
disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false"/>
</system.serviceModel>
争取客户端和服务器CONFIGS
<basicHttpBinding>
<binding name="BasicHttpBinding_IService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
安装/启用基本身份验证
您可能还需要安装和IIS应用基本身份验证。
转到“程序和功能”/“打开/关闭窗口功能”。 在IIS和安全性的某个位置启用“基本身份验证”。
我关闭并打开了IIS控制台,并且能够在身份验证设置下启用它。
这当然,如果对于开发测试,它会警告你没有SSL证书。
不允许你通过不安全的连接使用的用户名认证
您可以通过我用ClearUsernameBinding安全传输(例如SSL)或消息加密(使用证书)
确保消息在过去取得巨大成功,但我不推荐它在生产中。我使用它,以便我可以保持所有身份验证代码不变,而不需要在开发/测试环境中使用SSL,但只需更改配置即可使用SSL。
注意:该自定义绑定并不完美,我必须稍微更改它以启用某些配置更改。
这就是解决了我的问题:
<bindings>
<basicHttpBinding>
<binding>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
更多信息参见: https://msdn.microsoft.com/en-gb/library/ff648505.aspx
谢谢,这已经修复了认证错误,但是现在该服务进入了故障状态。跟踪日志中没有任何内容可以说明原因。 – TheRealTy