2017-03-07 16 views
1

下記のアプリを参照してください:例外= HTTP要求は、クライアントの認証方式 '匿名' と不正である

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ヘッダーに渡す必要があります。

答えて

2

私はこれらのステップに続い:

1)は、Webアプリケーション 2の認証設定に移動します)右の匿名認証をクリックし、[編集] 3を選択)ユーザーのパスワードを変更

にパスワードを指定最近変わった。私は、アプリケーションプールのパスワードを変更すると、それを並べ替えると思った。しかし、あなたはここでもそれをしなければなりません。

ここに私が使用したウェブページがあります:http://windows.vexedlogic.com/2013/08/03/401-2-unauthorized-invalid-authentication-headers-when-using-anonymous-authentication/

関連する問題