异常 = HTTP 请求未经客户端身份验证方案“匿名”授权

Posted

技术标签:

【中文标题】异常 = HTTP 请求未经客户端身份验证方案“匿名”授权【英文标题】:Exception = The HTTP request is unauthorized with client authentication scheme 'Anonymous' 【发布时间】:2017-03-07 17:20:14 【问题描述】:

请看下面的应用:

public Form1()
        
            try
            
                InitializeComponent();
                ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client();
                s1.ClientCredentials.UserName.UserName = "testuser";
                s1.ClientCredentials.UserName.Password = "testpass";
                string str = s1.GetData(1);
            
            catch (System.ServiceModel.Security.MessageSecurityException e)
            
                //Handle an authentication failure
                using (System.IO.StreamWriter file =
                   new System.IO.StreamWriter(@"C:\LogFile\log.txt"))
                
                    file.WriteLine("User Authentication Failed");
                
            

        

以及下面的 app.config:

<system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1">
                    <security mode="TransportWithMessageCredential">
                        <message clientCredentialType="UserName" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://hq-wk-is/WCFSSL/Service1.svc" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="WSHttpBinding_IService1" />
        </client>
    </system.serviceModel>

这是服务的 web.config:

 <system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint contract="WcfService1.IService1" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>      

        <!--added-->
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication  userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1.CustomUserNameValidator, WcfService1" />
          </serviceCredentials>
        </behavior>

        <!--<behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>-->
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>

    <!--added-->
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindingConfiguration" >
          <security mode="TransportWithMessageCredential" >
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

和 CustomValidator:

namespace WcfService1

    public class CustomUserNameValidator : UserNamePasswordValidator
    
        public override void Validate(string userName, string password)
        

            using (System.IO.StreamWriter file =
            new System.IO.StreamWriter(@"C:\log\log.txt"))
            
                file.WriteLine("Hola");
            



            if (null == userName || null == password)
            
                throw new ArgumentNullException();
            

            if (!(userName == "testuser" && password == "testpass"))
            
                // This throws an informative fault to the client.
                throw new FaultException("Unknown Username or Incorrect Password");
                // When you do not want to throw an infomative fault to the client,
                // throw the following exception.
                // throw new SecurityTokenException("Unknown Username or Incorrect Password");
            
        
    

我已在 IIS 中仅为此 Web 应用程序启用基本身份验证。

我得到的错误是:The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="MyPC"'

有什么问题?为什么要匿名发送凭据?在这种情况下,我相信凭据应该在 SOAP 标头中传递。

【问题讨论】:

【参考方案1】:

我按照以下步骤操作:

1) 转到 Web 应用程序的身份验证设置 2)右键单击匿名身份验证并选择编辑 3) 修改指定用户的密码

密码最近更改了。我认为更改应用程序池上的密码会对它进行排序。但是,您也必须在这里进行。

这是我使用的网页:http://windows.vexedlogic.com/2013/08/03/401-2-unauthorized-invalid-authentication-headers-when-using-anonymous-authentication/

【讨论】:

以上是关于异常 = HTTP 请求未经客户端身份验证方案“匿名”授权的主要内容,如果未能解决你的问题,请参考以下文章

HTTP 请求未经客户端身份验证方案“匿名”授权。使用 wsdl 文件

客户端身份验证方案“匿名”的 HTTP 请求未经授权?

HTTP 请求未经客户端身份验证方案“基本”授权。从服务器收到的身份验证标头为“基本领域 =”

HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“默认”。

HTTP 请求未经客户端身份验证方案“协商”授权

HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“基本领域”