4

.NET で Web サービス呼び出しに Cookie を設定する際に問題が発生しています。提供された wsdl の呼び出しを使用する前に、クライアント Web サイトへのログイン時に取得される Cookie を提供する必要があります。ログインして Cookie を取得するメソッドがあり、それを makeSearch メソッド (以下に示します) に渡します。ご覧のとおり、wsdl オブジェクトの cookieContainer に Cookie を設定しています。ただし、AdvancedSearch メソッドによって行われた要求を確認すると、フィドラーで Cookie が送信されていないことに気付きました。クライアントは Java でソリューションを提供しましたが、それを .NET に転送する際に問題が発生しています。

以下は Java コードでの解決策です: (port は渡された wsdl オブジェクトです)

private static void setupClient(Object port, final String cookie) throws Exception {
    Client client = ClientProxy.getClient(port); 
    HTTPConduit http = (HTTPConduit) client.getConduit();
    HTTPClientPolicy policy = http.getClient();
    if (policy == null) {
        policy = new HTTPClientPolicy();
        http.setClient(policy);
    }
    policy.setCookie(cookie);
    policy.setAutoRedirect(true);
}

私のコードは次のとおりです。

public AdvancedSearchResult makeSearch(String cookie) {
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
    AdvancedSearchResult searchResults = new AdvancedSearchResult();
    Cookie cook= new Cookie("NAME", HttpUtility.UrlEncode(cookie));
    searches.CookieContainer = new CookieContainer();
    searches.CookieContainer.Add(newUri(www.test.com),cook);
    searchResults = searches.AdvancedSearch("search params");
    return searchResults;
}

誰でもアイデアや解決策をリストできますか?

4

1 に答える 1

5

私はこれと同じ問題を抱えていましたが、これが私がそれを解決した方法です

var tmWebServices             = new TM_WebServices();
tmWebServices.CookieContainer = new System.Net.CookieContainer();
tmWebServices.Url             = Test_TM.tmWebServices;

var username     = "reader";  
var passwordHash = Test_TM.passwordHash_Reader;
var sessionID    =  tmWebServices.Login(username, passwordHash);

//Note that this is optional if you set these cookies from the server side
var cookie       =  new System.Net.Cookie("Session",sessionID.str());
tmWebServices.CookieContainer.Add(tmWebServices.Url.uri() , cookie); 

var guidanceItemID = "0c85a318-0c32-4417-9d72-7475bb96517e".guid();

var guidanceItemHtml = tmWebServices.GetGuidanceItemHtml(guidanceItemID);

return "{0}".format(guidanceItemHtml);


//using O2.SecurityInnovation.TeamMentor
//O2File:TM_WebServices.cs
//O2File:Test_TM_Config.cs
//O2Ref:System.Web.Services.dll

キーは、追加することでした

tmWebServices.CookieContainer = new System.Net.CookieContainer();

これにより、サーバーから送信された Cookie がその後の要求で再送信されるようになりました。

上記の例で、有効なセッション Cookie 値なしで GetGuidanceItemHtml を呼び出すと、セキュリティ例外が発生します (サーバー側で CAS セキュリティ要求があります)。

于 2012-01-17T14:23:24.640 に答える