Exception = HTTP请求未经授权使用客户端身份验证方案“Anonymous”

时间:2017-03-07 17:20:14

标签: c# wcf

请参阅以下应用:

public void saveMember()
{

  string pathMemberPk = Path.Combine(Environment.CurrentDirectory, @"../memberDataPk.bin");

  if (!File.Exists(pathMemberPk)) {

     memberPk = 0001;
     memberPkString = memberPk.ToString();

     IFormatter formatter = new BinaryFormatter();
     Stream streamPk = new FileStream(pathMemberPk, FileMode.Create);
     formatter.Serialize(streamPk,this.memberPkString);   

   }else{

     using (FileStream streamIn = File.OpenRead("f://MyFile.bin"))
     {
         IFormatter formatter = new BinaryFormatter();                   
         object obj = formatter.Deserialize(streamIn);
         memberPk = Convert.ToInt32(obj); //to INT
        memberPk = Convert.ToString(obj); //To String

         Console.WriteLine(memberPk);
      }

   }
}

和下面的app.config:

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");
                }
            }

        }

以下是该服务的web.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>

和CustomValidator:

 <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>

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

我得到的错误是: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"); } } } }

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

1 个答案:

答案 0 :(得分:2)

我按照以下步骤操作:

1)转到Web应用程序的身份验证设置 2)右键单击“匿名身​​份验证”,然后选择“编辑” 3)更改指定用户的密码

最近密码已更改。我以为更改应用程序池上的密码会对其进行排序。但是,你也必须在这里做。

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