在简单的测试Wcf服务中使用基本身份验证时遇到一些问题。我得到了一个例外:
无法激活请求的服务,无法激活'http://qld-tgower/test/Service.svc‘。有关详细信息,请参阅>服务器的诊断跟踪日志。
在跟踪日志中,它显示:
匿名主机上配置的身份验证方案(‘
’)不允许在绑定'BasicHttpBinding‘(’匿名‘)上配置的身份验证方案。请确保将SecurityMode设置为传输或TransportCredentialOnly。此外,可以通过以下方式解决此问题:通过IIS管理工具更改此应用程序的身份验证方案、通过ServiceHost.Authentication.AuthenticationSchemes属性、在应用程序配置文件中的元素处、通过更新绑定的ClientCredentialType属性或通过调整HttpTransportBindingElement的AuthenticationScheme属性。
但是,当我使用不正确的用户名和密码时,它说它正在使用基本身份验证,这是我不理解的吗?
使用客户端身份验证方案'Basic‘时,该请求未经授权。从服务器收到的身份验证头是“Basic realm=”qld-tgower“”。
This is my web.config details
<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);
}
}
我是不是漏掉了什么?
发布于 2011-11-29 10:39:48
更改服务的名称和约定以包括命名空间。
另外,删除端点地址(将其设置为""),并且不要在传输标记中包含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>
发布于 2011-11-22 08:48:19
尝试客户端和服务器配置
<basicHttpBinding>
<binding name="BasicHttpBinding_IService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
安装/启用基本身份验证
您可能还需要在IIS中安装和应用基本身份验证。
转到“程序和功能”/“打开/关闭windows功能”。在IIS和security下启用“基本身份验证”。
我关闭并打开了IIS控制台,并且能够在身份验证设置下启用它。
当然,如果是为了进行开发测试,它会警告您没有SSL证书。
发布于 2011-11-22 08:52:18
不允许在不安全的连接上使用用户名身份验证
您可以通过使用安全传输(例如SSL)或邮件加密(使用证书)来保护邮件
我过去使用ClearUsernameBinding取得了很大的成功,但我不推荐在生产中使用它。我之所以使用它,是因为我可以在dev/test环境中保持所有身份验证代码不变,而不需要SSL,只需更改配置即可使其与SSL一起工作。
注意:自定义绑定并不完美,我必须对其进行一些更改以启用某些配置更改。
https://stackoverflow.com/questions/8220555
复制相似问题