3

フォーム認証を使用してWP7(マンゴー)エミュレータを使用して私の作品ウェブサイトにログオンしようとしています。Windows Phone 7.1(マンゴー)でのフォーム認証?

<system.web.extensions> 
<scripting> 
    <webServices> 
     <authenticationService enabled="true" requireSSL="false"/> 
    </webServices> 
</scripting> 

彼らは基本的に認証ODataServiceを呼び出すことにより、ログインするサンプルコードがあります。これを含まWebConfigの

サイトは(最終的にあなたがこのサイトにいくつかのものを表示することができますWP7アプリを作成します)ユーザー/パスを渡し、認証Cookieを取得します。

このCookieを使用すると、すべての新しい要求がODataServiceに含まれ、データを取得できるはずです。

任意の要求が行われる前に - 認証Cookieを取得

private static string GetAuthenticationCookie(string username, string password) 
    { 
     if (authenticationCookie == null) 
     { 
      string loginServiceAddress = websiteUrl + "/Authentication_JSON_AppService.axd/Login"; 

      var request = CreateAuthenticationRequest(username, password, loginServiceAddress); 

      var response = request.GetResponse(); 
      if (response.Headers["Set-Cookie"] != null) 
      { 
       authenticationCookie = response.Headers["Set-Cookie"]; 
      } 
      else 
      { 
       throw new SecurityException("Invalid credentials"); 
      } 
     } 

     return authenticationCookie; 
    }  

private static WebRequest CreateAuthenticationRequest(string username, string password, string loginServiceAddress) 
    { 
     var request = HttpWebRequest.Create(loginServiceAddress); 

     request.Method = "POST"; 
     request.ContentType = "application/json"; 
     var body = string.Format(
      "{{ \"userName\": \"{0}\", \"password\": \"{1}\", \"createPersistentCookie\":false}}", 
      username, 
      password); 
     request.ContentLength = body.Length; 

     var stream = request.GetRequestStream(); 
     using (var sw = new StreamWriter(stream)) 
     { 
      sw.Write(body); 
     } 

     return request; 
    } 

I CANTちょうどネットワークコードが若干異なっているとして、コピー&ペーストのWindows Phone 7.1(マンゴー)アプリにこのコードを - 例えば..everythingはWP7で非同期に行われ、もはや

myWebRequest.GetResponse() 

のようなものworks..and私は

ような何かをしなければなりませんさ
myWebRequest.BeginGetResponse() 

誰でもWP7でフォーム認証を使用して認証を行う任意の作業コードサンプルがある場合 - 素晴らしいことだ

おかげで多くの

答えて

2

は、このHTTPクラスをチェックアウト:

http://mytoolkit.codeplex.com/wikipage?title=Http

次のコードはこのトリックを行うべきです:

private void OnButtonClick(object sender, RoutedEventArgs e) 
{ 
    const string username = "user"; 
    const string password = "password"; 
    const string loginServiceAddress = "http://www.server.com/Services/AuthenticationService.svc"; 

    var text = string.Format("{{ \"userName\": \"{0}\", \"password\": \"{1}\", \"createPersistentCookie\":false}}", 
     username, password); 

    var request = new HttpPostRequest(loginServiceAddress); 
    request.RawData = Encoding.UTF8.GetBytes(text); 
    request.ContentType = "application/json"; 
    Http.Post(request, AuthenticationCompleted); 
} 

private void AuthenticationCompleted(HttpResponse authResponse) 
{ 
    const string serviceAddress = "http://www.server.com"; 
    if (authResponse.Successful) 
    { 
     var sessionCookie = authResponse.Cookies.Single(c => c.Name == "SessionId"); 
     var request = new HttpGetRequest(serviceAddress); 
     request.Cookies.Add(new Cookie("SessionId", sessionCookie.Value)); 
     Http.Get(request, OperationCallCompleted); 
    } 
} 

private void OperationCallCompleted(HttpResponse response) 
{ 
    if (response.Successful) 
    { 
     // TODO: do your parsing 
     var responseText = response.Response; 

     Deployment.Current.Dispatcher.BeginInvoke(() => 
     { 
      // TODO: set data to binding or directly to user control (in UI thread) 
     }); 
    } 
} 
+1

コードを使用することができません - コンパイルエラー - 一部のクラスが不足していますHttpUtilityExtensionsが原因です - – Jerrold

+2

HttpUtilityExtensionsはここにあります:https://mytoolkit.svn.codeplex.com/ svn/Utilities/HttpUtilityExtensions.cs –

+1

編集:https://mytoolkit.svn.codeplex.com/svn/Shared/Utilities/HttpUtilityExtensions.cs –

関連する問題