Windows フォーム アプリケーションから WP8 にいくつかのコードを移植しようとしていますが、非同期呼び出しに関するいくつかの問題に遭遇しました。基本的な考え方は、いくつかの UAG 認証を行うことです。Windows フォーム コードでは、ポータル ホームページで GET を実行し、Cookie を待ちます。次に、これらの Cookie を UAG サーバーの検証 URL への POST 要求に渡します。すべてのステップが連続的かつ同期的であるため、フォーム内ではすべて正常に機能します。
これを WP8 に移植し始めたとき、最初に気付いたのは、GetResponse() が利用できないことでした。代わりに、非同期でコールバック関数を呼び出す BeginGetResponse() を使用する必要がありました。POST を実行する前に、このステップが確実に終了するようにする必要があるため、これは私にとっては良くありません。
私の Windows フォーム コードは次のようになります ( http://usingnat.net/sharepoint/2011/2/23/how-to-programmatically-authenticate-to-uag-protected-sharep.htmlから取得):
private void Connect()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Url);
request.CookieContainer = new CookieContainer();
request.UserAgent = this.UserAgent;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//Get the UAG generated cookies from the response
this.Cookies = response.Cookies;
}
}
private void ValidateCredentials()
{
//Some code to construct the headers goes here...
HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(this.ValidationUrl);
postRequest.ContentType = "application/x-www-form-urlencoded";
postRequest.CookieContainer = new CookieContainer();
foreach (Cookie cookie in this.Cookies)
{
postRequest.CookieContainer.Add(cookie);
}
postRequest.Method = "POST";
postRequest.AllowAutoRedirect = true;
using (Stream newStream = postRequest.GetRequestStream())
{
newStream.Write(data, 0, data.Length);
}
using (HttpWebResponse response = (HttpWebResponse)postRequest.GetResponse())
{
this.Cookies = response.Cookies;
}
public CookieCollection Authenticate()
{
this.Connect();
this.ValidateCredentials();
return this.Cookies;
}
問題は、このコードが同期操作 (最初に Connect() を呼び出し、次に ValidateCredentials() を呼び出す) に依存していることです。WP8 は Web 要求に対してそれをサポートしていないようです。2 つの機能を 1 つに結合することもできますが、それでは問題を完全には解決できません。後で UAG の背後にあるリソースにアクセスするためにこれを拡張する必要があるため、モジュール設計が必要になるからです。
同期を「強制」する方法はありますか?
ありがとう