2016-09-28 3 views
0

SOAP 서비스를 사용하려고하는데 인증 문제가있어서 유용하지 않은 것으로 생각됩니다. 나는이 문제가 내가 자격 증명 (단순히 내가해야 할 일이라고 생각하지만 단지 상황을보다 독특하게 만든다고 생각하는 것) 이상으로 헤더에 전달할 수 있다고 생각한다. 아래는 설정 파일과 인증에 사용하는 코드입니다.Wcf 기본 인증 사용자 이름을 수락하지 않습니다.

<?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을 남겨,하지만 난에 대해 주로 걱정 하나의 이름 = "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; 
} 

여기에 '잘못된 웹 서비스 자격 증명 사용됨'이 표시됩니다. 그래서 독서 후에 나는 첫 번째 방법을 이것을 바꿨다.

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에서 username을 지정하십시오. '오류가 발생합니다. 그래서 지금 나는 내가 전에 있었던 곳으로 더 멀리 움직이고있는 것처럼 느낍니다. 어쩌면 누군가가 이것에 대해 밝힐 수 있을까요? 미리 감사드립니다!

답변

0

나는 그것을 알아 냈다. 나는 내 코드에서 다른 클라이언트를 멍청하게 참조한다. 간과하는 것은 항상 간단한 실수입니다.

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