Wcf基本认证不接受用户名

本文关键字:用户 不接受 认证 Wcf | 更新日期: 2023-09-27 18:09:13

我正在尝试使用SOAP服务,并且遇到身份验证问题,我认为我可能错过了一些有用的东西。我认为问题可能在于我传递的标题不仅仅是凭据(我相信这是我必须做的,但我认为这只会使情况更加独特)。下面是我的配置文件和我用来验证的代码。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MemberSoap">
          <security mode="Transport" />
        </binding>
        <binding name="MemberSoap1" />
        <binding name="TransactionSoap">
          <security mode="Transport" />
        </binding>
        <binding name="TransactionSoap1" />
        <binding name="CertificateSoap" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
          <readerQuotas maxDepth="32" maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
          <security mode="Transport" />
        </binding>
        <binding name="CertificateSoap1" />
        <binding name="MembershipSoap">
          <security mode="Transport">
            <transport clientCredentialType="Basic" proxyCredentialType="None" realm="" />
            <message clientCredentialType="Certificate" algorithmSuite="Default" />
          </security>
        </binding>
        <binding name="ContentSoap">
          <security mode="Transport" />
        </binding>
        <binding name="ContentSoap1" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://membership/member.asmx"
        binding="basicHttpBinding" bindingConfiguration="MemberSoap"
        contract="Member.MembershipWsMemberSoap" name="Ovs.Membership.Ws.MemberSoap" />
      <endpoint address="https://membership/transaction.asmx"
        binding="basicHttpBinding" bindingConfiguration="TransactionSoap"
        contract="MembershipWsTransactionSoap" name="TransactionSoap" />
      <endpoint address="https://membership/certificate.asmx"
        binding="basicHttpBinding" bindingConfiguration="CertificateSoap"
        contract="MembershipWsCertificateSoap" name="CertificateSoap" />
      <endpoint address="https://ngmembership/Membership.svc"
        binding="basicHttpBinding" bindingConfiguration="MembershipSoap"
        contract="Membership.IMembership" name="MembershipSoap" />
      <endpoint address="https://membership/content.asmx"
        binding="basicHttpBinding" bindingConfiguration="ContentSoap"
        contract="Content.MembershipWsContentSoap" name="ContentSoap" />
    </client>
  </system.serviceModel>
    <appSettings>
      <add key="UsernameAuth" value="user" />
      <add key="PasswordAuth" value="pass" />
    </appSettings>
</configuration>
出于安全考虑,我省略了基本url和完整的名称空间,但我主要关心的是name="MembershipSoap"服务。这是我第一次尝试使用用于身份验证的代码。
public Transaction[] GetAllBookingInfo(string memberId, string partnerId)
{
    AllBookingsByMemberIdRS response;
    using (var client = new MembershipClient())
    using (new OperationContextScope(client.InnerChannel))
    {
        // add the basic auth header
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name]
            = GetBasicAuthHeader("user", "pass");
        var request = new AllBookingsByMemberIdRQ
        {
            MemberId = memberId,
            PartnerId = partnerId
        };
        response = AuthenticateServiceUser.membershipSession.GetAllBookingsByMemberId(request);
    }
    var trans = response.Transactions;
    return trans;
}
protected HttpRequestMessageProperty GetBasicAuthHeader(string userName, string password)
{
    // get the basic auth header
    HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
    httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + password));
    return httpRequestProperty;
}

此时,我得到一个错误,说有"使用无效的web服务凭据"。所以读完后,我把第一种方法换成了这个。

public Transaction[] GetAllBookingInfo(string memberId, string partnerId)
{
    AllBookingsByMemberIdRS response;
    var client = new MembershipClient();
    client.ClientCredentials.UserName.UserName = "user";
    client.ClientCredentials.UserName.Password = "pass";
    using (new OperationContextScope(client.InnerChannel))
    {
        var request = new AllBookingsByMemberIdRQ
        {
            MemberId = memberId,
            PartnerId = partnerId
        };
        response = AuthenticateServiceUser.membershipSession.GetAllBookingsByMemberId(request);
    }
    var trans = response.Transactions;
    return trans;
}

现在我得到一个'用户名未提供。在ClientCredentials错误中指定用户名。所以现在我觉得我离以前的地方越来越远了。也许有人能解释一下?提前感谢!

Wcf基本认证不接受用户名

我明白了。我愚蠢地在代码中引用了另一个客户机——一个没有凭据的客户机。总是那些简单的错误被忽略了。

public Transaction[] GetAllBookingInfo(string memberId, string partnerId)
{
    AllBookingsByMemberIdRS response;
    using (var client = new MembershipClient())
    using (new OperationContextScope(client.InnerChannel))
    {
        // add the basic auth header
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name]
            = GetBasicAuthHeader("user", "pass");
        var request = new AllBookingsByMemberIdRQ
        {
            MemberId = memberId,
            PartnerId = partnerId
        };
        response = client.GetAllBookingsByMemberId(request);
    }
    return response.Transactions;
}