WCF-TransportWithMessageCredential HTTP 请求未经授权,客户端身份验证方案“匿名”
Posted
技术标签:
【中文标题】WCF-TransportWithMessageCredential HTTP 请求未经授权,客户端身份验证方案“匿名”【英文标题】:WCF-TransportWithMessageCredential The HTTP request is unauthorized with client authentication scheme 'Anonymous' 【发布时间】:2019-09-13 00:18:02 【问题描述】:我正在尝试使用 UserName 配置 WCF 身份验证,但没有成功,我已经尝试了很多我已经找到的解决方案,但似乎没有什么对我有用。
HTTP 请求未经客户端身份验证方案授权 '匿名的'。从服务器收到的身份验证标头是 'Negotiate,NTLM,Basic realm="localhost"'.
服务器设置
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="basicHttpBindingConfiguration">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" negotiateServiceCredential="false" />
<transport clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="Namespace.Service" behaviorConfiguration="wsHttpBehavior">
<endpoint binding="wsHttpBinding" bindingConfiguration="basicHttpBindingConfiguration" name="Soap" bindingNamespace="/WebServices" contract="Namespace.IService">
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" name="mex" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="wsHttpBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="UserNamePasswordValidator, WebApplication" />
<!--<serviceCertificate findValue="ServiceModelSamples-HTTPS-Server" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />-->
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
在客户端
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Soap">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost:85/WebServices/Service.svc"
binding="wsHttpBinding" bindingConfiguration="Soap" contract="ServiceReference1.IService"
name="Soap" />
</client>
</system.serviceModel>
这是我的 IIS 配置
我发现的大多数解决方案都使用 Windows 传输,我只需要通过用户名进行身份验证。
我检查一下:
https://blog.sandervanlooveren.be/posts/securing-a-wcf-service-with-username-and-password/ https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/ws-transport-with-message-credential我尝试了安全 mode="Message" 和安全 mode="TransportWithMessageCredential" 均未成功
我的客户端代码如下:
// Instantiate the proxy
var proxy = new ServiceClient();
proxy.ClientCredentials.UserName.UserName = "username";
proxy.ClientCredentials.UserName.Password = "password";
请任何人检查我的配置并告诉我设置错误吗?
编辑: 这是 ASP.Net MVC5 应用程序中的一个服务,这也是一个问题吗?
【问题讨论】:
【参考方案1】:customUserNamePasswordValidatorType 属性字符串可能有问题。通常格式如下
customUserNamePasswordValidatorType="Namespace.MyCustUserNamePasswordVal,Namespace"
您可以访问托管服务的 WSDL 吗?另外,开启匿名认证就足够了,不需要开启其他认证。 这是我的配置,我使用了WCF4.5支持的简化配置,希望对你有用。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<userNameAuthentication customUserNamePasswordValidatorType="WcfService1.CustUserNamePasswordVal,WcfService1" userNamePasswordValidationMode="Custom"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding>
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<protocolMapping>
<add binding="wsHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
如果有什么我可以帮忙的,请随时告诉我。
【讨论】:
感谢您的回答。对于 customUserNamePasswordValidatorType,我认为这里可以 customUserNamePasswordValidatorType="Namespace.MyCustUserNamePasswordVal, AssemblyName" 。我现在有这个以上是关于WCF-TransportWithMessageCredential HTTP 请求未经授权,客户端身份验证方案“匿名”的主要内容,如果未能解决你的问题,请参考以下文章