简体   繁体   中英

Connecting to a SOAP WSDL in Visual Studio C#

I am trying to connet my C# console application with a thridparty SOAP WSDL API however I cannot get it working. I connect to the service with the WSDL string as a Service recourse in Visual studio: https://domain.tld/SimpleAPI/SimpleAPIService.svc?singleWsdl

The documentation says: Authentication is sessions based. The requester must provide a valid session token in the Authenticate field of each request. A session has access to the same resources and capabilities as the given user. To get a session token, use the GetSessionToken method. To validate a session, use the PingPong method. The documentation focuses on the REST API, which is not an option for me. The two methods for connecting is as follows:

curl \ -H "Content-Type: application/json" \ -X POST -d '{ "username": "admin@domain.com", "password": "VerySecure123" }' \ https://domain.tld/SimpleAPI/SimpleAPIService.svc/rest/GetSessionToken

This should return something like this: C9C5MlqrpMwG/6bQ3mAVlm0Z6hi/eh1vsQs4i1I/g== This part is okay, I get a token back. However when I try the PingPong method, I get an error saying that it is a wrong token or the session has expired.

curl \ -H "Content-Type: application/json" \ -H "Authenticate:CCPSessionId C9C5MlqrpMwG/6bQ3mAVlm0Z6hi/eh1vsQs4i1I/g==" \ -X POST -d '{}' \ https://domain.tld/SimpleAPI/SimpleAPIService.svc/rest/PingPong

My code is as follows:

static void Main(string[] args)
        {
            string apiUsername = "user";
            string apiPassword = "pass";

            using (var client = new acmp.SimpleAPIServiceClient())
            {
                client.ClientCredentials.UserName.UserName = apiUsername;
                client.ClientCredentials.UserName.Password = apiPassword;

                string token = client.GetSessionToken(apiUsername, apiPassword);
                string tokenText = string.Format("CCPSessionId {0}", token);

                using (OperationContextScope contextScope = new OperationContextScope(client.InnerChannel))
                {
                    var httpRequestProperty = new HttpRequestMessageProperty();

                    httpRequestProperty.Headers[HttpRequestHeader.Authorization] = token; //tokenText

                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                    client.PingPong();
                }
            }

            Console.WriteLine("End...");
            Console.ReadLine();
        }

The theory right now is that my script is not passing the correct header.

Please help, we have spend so much time debugging.

Did a little reading up, and I can't find a token text of "CCPSessionsid " anywhere.

I see "bearer", token.AccessToken

Along the lines of:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);

I also came across this issue while working with the same API. Cutting the quotation marks off the string solved this for me.

request.Headers.TryAddWithoutValidation("Authenticate", token.Trim(new Char[] {'"'}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM