开发者

Setting up an authorized connection between a php webservice and a C# WCF client

开发者 https://www.devze.com 2023-03-17 04:14 出处:网络
Our php webservice provides webfunctions for clients. We have used the following code to do the authentication.

Our php webservice provides webfunctions for clients. We have used the following code to do the authentication.

$client = new SoapClient("some.wsdl", 
                         array('login' => "some_name",
                               'password' => "some_password"
                         ));

The client has be开发者_如何学Goen build in C# and we have been able to set up a connection without authentication but we haven't been able to apply the authentication.

what kind of properties should we provide for the clients? I have tried this configuration in combination with the username credentials.

        <basicHttpBinding>
            <binding name="webserviceControllerBinding">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Basic"/>
                </security>
            </binding>
        </basicHttpBinding>

client.ClientCredentials .UserName.UserName = "username"; client.ClientCredentials.UserName.Password = "password";

What kind of security is our php service using? How can we set-up a reliable connection with authentication but without using certificates or https.


It seems I have to put the authentication in the soapheader.
http://ericphan.info/blog/2010/6/3/adding-custom-http-header-to-all-wcf-requests.html
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c96c8f6e-0711-4ba1-a97c-008f44610f0e/
http://www.codeproject.com/KB/webservices/SOAPHeaderAuthentication.aspx
Going by some of the examples on the internet.
But i haven't found a example for my case jet.


         MessageHeader header= MessageHeader.CreateHeader("My-CustomHeader","http://myurl","Custom Header.");

           using (phptoebiTestclient.ServiceReference1.webserviceControllerPortTypeClient client = new phptoebiTestclient.ServiceReference1.webserviceControllerPortTypeClient())
           {
               using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
               {
                   OperationContext.Current.OutgoingMessageHeaders.Add(header);
                   HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
                   byte[] toEncodeAsBytes    = System.Text.ASCIIEncoding.ASCII.GetBytes("username:password");                       
                   httpRequestProperty.Headers.Add("Authorization: Basic "+ System.Convert.ToBase64String(toEncodeAsBytes));                                              
                   OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                   string str = client.getVacanciesReference("");
               }                                 
           }

Code above has solved my problem (I used these URLs for reference)

http://caught-in-a-web.blogspot.com/2008/04/how-to-add-custom-http-header-in-wcf.html
http://en.wikipedia.org/wiki/Basic_access_authentication


Try

<security mode="TransportWithMessageCredential">
   <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

UPDATED: It should be TransportWithMessageCredential mode

0

精彩评论

暂无评论...
验证码 换一张
取 消