In .NET, how do you provide the session identifier to a web service that requires authentication?

I am using a web service that requires authentication from .NET (Visual Studio 2010). According to the documentation, you first request a session id from the first web service. I can do it without a problem. Then you must call the second web service to actually fulfill your request, passing the session ID to the cookie. Here is my sample code:

AuthenticateService authenticate_service = new AuthenticateService();
string session_identifier = authenticate_service.Authenticate();

SearchService search_service = new SearchService();
search_service.CookieContainer = new CookieContainer();
Cookie cookie = new Cookie("Cookie", "SID=" + session_identifier, null, search_service.Url);
search_service.CookieContainer.Add(cookie);

search_service.Test();

However, on the last line, I get the following exception:

System.Web.Services.Protocols.SoapException was unhandled The value of the cookie Message = Session ID cannot be empty or empty. The high-level web service client program is required to participate in a server-initiated session.

- , cookie -?

+3
2

...

domain Cookie. search_service.Url, , . -, - "search.google.com". , , .

+1

- . -, cookie, . WebClient cookie.

public class CookieAwareWebClient : WebClient
{

    private CookieContainer m_container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = m_container;
        }
        return request;
    }
}

WebClient, cookie .

:

CookieAwareWebClient webClient = new CookieAwareWebClient();    
NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
data["user"] = "myusername"; //now holds a user=username map
byte[] response = webClient.UploadValues("http://localhost:8080/somewebservice/auth/","POST", data); //point to the webservice authentication URI

webclient.UploadFile UploadValues , , , .

0

Source: https://habr.com/ru/post/1770180/


All Articles