2016-04-19 11 views
0

Facebookにログインできるようにidentityserverを設定しようとしています。 facebook.Theのmvcアプリからユーザー情報のいずれかを取得することはできません以外はすべて正常に動作しますgivennameと姓のクレームが期待されています。私はpublic_profileにスコープを追加しましたが、それを戻しません。Identity Server 3を使用した外部ログイン - facebookから指定された名前のクレームを設定する

多くの研究の末、これを行うためにUserServiceをセットアップする必要があるようですが、これを行う方法がわかりません。

助けていただければ幸いです。ここで

はidenityサーバアプリのstatupで外部プロバイダを設定するには、私のコードです:

private void ConfigureIdenityProviders (IAppBuilder app, string signInAsType) 
    { 

     var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions 
     { 
      AuthenticationType = "Facebook", 
      Caption = "Sign in with Facebook", 
      SignInAsAuthenticationType = signInAsType, 
      AppId = "xxxxxxxxxxxx", 
      AppSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 



      Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider() 
      { 
       OnAuthenticated = (context) => 
       { 

        // Problem here - there is no last_name returned 
         JToken lastname; 
        if (context.User.TryGetValue("last_name", out lastname)) 
        { 
         context.Identity.AddClaim(new System.Security.Claims.Claim(IdentityServer3.Core.Constants.ClaimTypes.FamilyName, lastname.ToString())); 
        } 

        context.Identity.AddClaim(new System.Security.Claims.Claim("role", "Guest")); 

        return Task.FromResult(0); 

       } 

      } 
     }; 

     facebookOptions.Scope.Add("public_profile"); 
     facebookOptions.Scope.Add("email"); 


     app.UseFacebookAuthentication(facebookOptions); 

    } 

答えて

0

私はちょうどfacebookグラフを使用してユーザープロファイルを取得しました。

ユーザーサービスがこれを行う別の方法かもしれませんが、これは私のために働きます。

using (var client = new HttpClient()) 
        { 
         var result = client.GetAsync("https://graph.facebook.com/me?fields=first_name,last_name&access_token=" + context.AccessToken).Result; 


         if (result.IsSuccessStatusCode) 
         { 
          var userInformation = result.Content.ReadAsStringAsync().Result; 
          var fbUser = JsonConvert.DeserializeObject<FacebookUser>(userInformation); 
          context.Identity.AddClaim(new System.Security.Claims.Claim(IdentityServer3.Core.Constants.ClaimTypes.GivenName, fbUser.first_name)); 
          context.Identity.AddClaim(new System.Security.Claims.Claim(IdentityServer3.Core.Constants.ClaimTypes.FamilyName, fbUser.last_name)); 
          context.Identity.AddClaim(new System.Security.Claims.Claim("role", "Guest")); 
         } 

        } 
0

あなたが戻ってのFacebookから取得する任意のクレームは、カスタム・ユーザー・サービスのAuthenticateExternalメソッドに渡されます。その時点で、彼らはあなたがしたいことを何でもしたいというあなたの主張です。

Facebookから欲しいクレームが得られない場合は、最初に問題を整理するためにスタンドアロンのMVCアプリを作成することをおすすめします。

0

OnAuthenticated = context => 
       { 
context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); 
return Task.FromResult(true); 
    } 

を次のようにトークンとトークンの使用コールFBを取得し、GIVEN_NAME

を取得した後、あなたのプロバイダを変更しない限り、あなたはFBからアクセストークンを取得することはありません
関連する問題