2016-06-19 14 views
1

私はASP.NET MVC Coreを初めて使用しています。これは、Facebookアカウントでユーザーにログインすることです。ユーザーは私たちのアプリケーションにログインし、クレーム(姓と名字、電子メールなど)を得ることができます。 Facebookのログインを有効にする方法を知っています。ASP.NET MVC Core(MVC 6)Facebookのログインとクレームを取得

FacebookOptions facebookOptions = new FacebookOptions(); 
facebookOptions.Scope.Add("email"); 
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"]; 
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; 
applicationBuilder.UseFacebookAuthentication(facebookOptions); 

applicationBuilder.UseIdentity().UseFacebookAuthentication(); 

私はクレームを得る方法とそのコードをどこに置くべきかについて助けが必要です。

ありがとうございました

+0

そして?あなたはそれを働かせるか、どこに問題を起こしましたか?私たちに助けてくれるかもしれません... –

+0

私たちは別の仕事の流れを決めました threxのためのthrx – Wasyster

答えて

2

あなたは身分証明書を使用しています。しかし、あなたの質問は、facebookアカウントからどのようにクレームを取得するかであり、それらを保持する方法ではありません。だから私は助けてくれるといい。私はCookieのミドルウェアでやっています。 ClaimsPrincipalをFacebookにログインさせるために 'temp' Cookieミドルウェアを追加し、次に、本当のCookieミドルウェアにサインインして、豊富なClaimsPrincipalを維持しました。起動クラスの設定メソッドのコードの関連作品:facebookOptionsのSignInSchemeは「温度」で、「TEMP」クッキーミドルウェアのオプションを使用するので、それはfalseにAutomaticAuthenticate(だ持っているか

  app.UseCookieAuthentication(
      new CookieAuthenticationOptions() 
      { 
       AuthenticationScheme = "Cookie", 
       AutomaticAuthenticate = true, 
       AutomaticChallenge = true, 
       LoginPath = new PathString(@"/account/login"), 
       AccessDeniedPath = new PathString(@"/account/accessdenied") 
      }); 

     app.UseCookieAuthentication(
      new CookieAuthenticationOptions() 
      { 
       AuthenticationScheme = "Temp", 
       AutomaticAuthenticate = false 
      }); 

     var facebookOptions = new FacebookOptions() 
     { 
      AuthenticationScheme = "Facebook", 
      SignInScheme = "Temp", 
      AppId = "yourappidhere", 
      AppSecret = "yourappsecrethere" 
     }; 
     facebookOptions.Scope.Add("public_profile"); 
     facebookOptions.Scope.Add("email"); 

     app.UseFacebookAuthentication(facebookOptions); 

は注意cookiePrincipleをtempクッキーに自動的に永続させたくないが、ここでは「クッキー」と呼ばれる実際のものすべてを豊かにしたい)。 LoginFacebookがあなたのページか何かのリンクを介して呼び出されたか

 public async Task<IActionResult> Register(string returnUrl = null) 
    { 
     var externalPrincipal = await HttpContext.Authentication.AuthenticateAsync("Temp"); 

     //TODO Check external principal and retrieve claims from db or whatever needs to be done here. 

     var claims = new List<Claim>() 
      { 
       new Claim("email", externalPrincipal.FindFirst(ClaimTypes.Email).Value) 
      }; 
     var id = new ClaimsIdentity(claims, "password"); 
     await HttpContext.Authentication.SignInAsync("Cookie", new ClaimsPrincipal(id)); 
     await HttpContext.Authentication.SignOutAsync("Temp"); 

     return Redirect(returnUrl); 
    } 

    public async Task<IActionResult> LogInFacebook(string returnUrl = null) 
    { 
     var queryString = !string.IsNullOrWhiteSpace(returnUrl) ? $"?returnUrl={returnUrl}" : string.Empty; 
     var props = new AuthenticationProperties() { RedirectUri = [email protected]"Account/Register{queryString}" }; //new PathString(returnUrl) 

     return await Task.Run<ChallengeResult>(() => new ChallengeResult("Facebook", props)); 

    } 

注:

はその後、私のコントローラ内の関連するメソッドは次のようになり。この時点でFacebookミドルウェアのSignInSchemeがどのように "Temp"であるかを覚えておいてください。それは "登録"アクションメソッドにリダイレクトされます。そこには、コードでのFacebookからClaimsPrincipleを抽出します。

var externalPrincipal = await HttpContext.Authentication.AuthenticateAsync("Temp"); 

この時点で、あなたが主張して行う必要があるものは何でも行うことができます。私はあなたが見ることができるように電子メールの主張を抽出します。また、CookiePrincipleをクッキーに保持するために、「Cookie」サインイン方式でサインインします。

+0

thnx、私はそれを試してみてください – Wasyster

+0

どうすれば私のWebAPIからFacebookのログインをターゲットできますか? – Wasyster

+0

私が推測する質問は分かりません。必要に応じてLogInFacebookを呼び出すことができます。 Web APIコントローラとMVC 6の通常のコントローラの違いはありません。 –

2

Facebookを使用してログインした後、そのアカウントに関連付けられているクレームをAccountControllers ExternalLoginCallbackメソッドで取得できます。

var email = info.Principal.FindFirstValue(ClaimTypes.Email); 
var name = info.Principal.FindFirstValue(ClaimTypes.GivenName) ?? 
         info.Principal.FindFirstValue(ClaimTypes.Name); 
var lastName = info.Principal.FindFirstValue(ClaimTypes.Surname); 
+0

答えのためのthnx – Wasyster

関連する問題