2017-09-14 7 views
0

PCLを使用してアプリケーションを使用すると、ユーザーはFacebookにログインした後にアプリケーションを使用できます。私は多くの研究を行ってきましたが、ログインした各ユーザーに一意のトークンを割り当て、WebApiを使用してユーザーをデータベースに保存する方法を見つけられませんでした。このトークンは、アプリ内のユーザー固有の詳細を識別する主なキーになります。これをやり遂げる方法についての助けに感謝します。ありがとう。以下はXamarin.Formsのログインユーザーにトークンを割り当てるPCL

あなたは、デバイス上のネイティブの安全なストレージにトークンを保存するために彼らのAPIを使用できることを意味しますXamarin.Authプラグインを使用しているように見えますドロイド

namespace LoyaltyWorx.Droid 
{ 
    public class FacebookRender : PageRenderer 
    { 
     public FacebookRender() 
     { 
      var activity = this.Context as Activity; 

      var auth = new OAuth2Authenticator(
       clientId: //my app's client id, 
       scope: "", 
       authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth/"), 
       redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html") 
       ); 

      auth.Completed += async (sender, eventArgs) => 
       { 
        if (eventArgs.IsAuthenticated) 
        { 
         var accessToken = eventArgs.Account.Properties["access_token"].ToString(); 
         var expiresIn = Convert.ToDouble(eventArgs.Account.Properties["expires_in"]); 
         var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn); 

         var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, eventArgs.Account); 
         var response = await request.GetResponseAsync(); 
         var obj = JObject.Parse(response.GetResponseText()); 

         var id = obj["id"].ToString().Replace("\"", ""); 
         var name = obj["name"].ToString().Replace("\"", ""); 

         App.NavigateToProfile(string.Format(name)); 
        } 
        else 
        { 
         App.NavigateToProfile("Invalid Login"); 
        } 
       }; 
      activity.StartActivity(auth.GetUI(activity)); 
     } 
    } 
} 

答えて

0

で私FacebookRenderクラスです。

if (eventArgs.IsAuthenticated) { 

    /* Just save the whole account here, you can also add custom properties to it if you want */ 
    await AccountStore.Create().SaveAsync(eventArgs.Account, "FacebookProviderKey"); 




    var accessToken = eventArgs.Account.Properties["access_token"].ToString(); 
    var expiresIn = Convert.ToDouble(eventArgs.Account.Properties["expires_in"]); 
    var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn); 

    var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, eventArgs.Account); 
    var response = await request.GetResponseAsync(); 
    var obj = JObject.Parse(response.GetResponseText()); 

    var id = obj["id"].ToString().Replace("\"", ""); 
    var name = obj["name"].ToString().Replace("\"", ""); 

    App.NavigateToProfile(string.Format(name)); 
} 

*編集:DBにデータを格納するには、Web APIエンドポイントへのREST呼び出しを行うだけです。あなたはとても似ている残りのコールを作るためにあなたのモバイルアプリからHttpClientを使用することができます。

public async Task SendData(Account accountInfo) { 

    HttpContent content = new StringContent(await Task.Run(() => JsonConvert.SerializeObject(accountInfo), cancellationToken).ConfigureAwait(false)); //Turn your Account data into a string to be sent to Web API 
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); //Tell what kind of string it is, it is JSON in this case 

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://someurl.com/Where/Web/Api/Lives") { 
      Content = content 
    }; //Change the URL to your Web API URL, add data, and set as POST request 

    using(HttpClient client = new HttpClient()) 
    using(HttpResponseMessage message = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, new CancellationTokenSource(TimeSpan.FromMinutes(1)).Token).ConfigureAwait(false)) { //Send the actual message 

     if(message.IsSuccessStatusCode) { 
      //Celebrate your success! 
     } else { 
      //You failed, sucks to suck 
     } 
    } 
} 
+0

ありがとうございました。私はトークンをユーザーの名前と電子メールと共にデータベースに保存する必要がありますが、私は既存のwebApiを持っていますので、どのようにしてユーザーを自分のデータベースに保存できますか?申し訳ありませんがこれはあまりにも多くを求めていることを知っていますが、私はXamarinとWebApiにとって非常に新しいです。これを達成するために私が見なければならない場所へのちょうどリンクはうまくいくでしょう。私は多くを研究しましたが、これを行う方法を見つけることができませんでした。 –

+0

@Unicorn_Girl編集を参照してください – hvaughan3

+0

ありがとう!私はこれを試みたが、私のデータベースに挿入されていない。 :(どこから取り消しトークンを取得していますか?残してもかまいません。 –

0

access_tokenが悪いセキュリティの実践と考えられている保存します。より良い方法はrefresh_tokenを保存することですが、Xamarin.AuthでAccountStoreを使用することも可能です。

関連する問題