使用简单的测试Wcf服务使用基本身份验证时遇到一些麻烦。我得到一个例外:
无法激活所请求的服务'http://qld-tgower/test/Service.svc'。见>服务器的诊断跟踪日志以获取更多信息。
在跟踪日志中显示:
主机上配置的身份验证方案('Basic')不允许在绑定'BasicHttpBinding'('Anonymous')上配置的身份验证方案。请确保将SecurityMode设置为Transport或TransportCredentialOnly。此外,可以通过IIS管理工具,在< serviceAuthenticationManager>的应用程序配置文件中通过ServiceHost.Authentication.AuthenticationSchemes属性更改此应用程序的身份验证方案来解决此问题。 element,通过更新绑定上的ClientCredentialType属性,或通过调整HttpTransportBindingElement上的AuthenticationScheme属性。
但是,当我使用不正确的用户名和密码时,我不明白它使用基本身份验证 IS 吗?
HTTP请求未经授权使用客户端身份验证方案“Basic”。从服务器收到的验证头是'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);
}
}
我在这里缺少什么东西吗?
答案 0 :(得分:19)
更改服务的名称和合同以包含命名空间。
此外,删除端点地址(将其设置为“”)并且不在传输标记中包含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>
答案 1 :(得分:3)
尝试客户端和服务器配置
<basicHttpBinding>
<binding name="BasicHttpBinding_IService">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
安装/启用基本身份验证
您可能还需要在IIS中安装和应用基本身份验证。
转到“程序和功能”/“打开/关闭Windows功能”。 在IIS和安全性下的某处启用“基本身份验证”。
我关闭并打开了IIS控制台,并能够在身份验证设置下启用它。
当然,如果进行开发测试,它会警告您没有SSL证书。
答案 2 :(得分:1)
您不能在不安全的连接上使用用户名验证
您可以使用安全传输(例如SSL)或邮件加密(使用证书)来保护邮件
我过去曾使用过ClearUsernameBinding取得了巨大成功,但我不建议将它投入生产。我使用它,以便我可以保持所有的身份验证代码相同,而不需要在开发/测试环境中使用SSL,但只需更改配置即可使用SSL。
注意:自定义绑定并不完美,我必须稍微更改一下才能启用某些配置更改。
答案 3 :(得分:1)
这就是我解决这个问题的原因:
<bindings>
<basicHttpBinding>
<binding>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
供参考,请参阅: https://msdn.microsoft.com/en-gb/library/ff648505.aspx