2016-07-14 15 views
3

私はMicrosoft.AspNetCore.Authentication.Googleパッケージを使用しています。ユーザーがGoogleで認証できるようにすると、私は自分のフォームを注入して、Googleがアクセスできない情報(たとえばカスタムID)を収集する必要があります。外部から認証した後、ユーザーからより多くの情報を収集するにはどうすればよいですか?

  1. 私はフォームを提示する必要がありますか?ログインを許可するためにオフになっている間にデータを前面に収集してセッションなどに保存しますか?

  2. ログインを許可する必要がありますか?コールバックURLが呼び出されたら、そこにフォームを表示しますか?

ミドルウェアを介して公開4つのイベントがあります。

  1. OnTicketReceived
  2. OnCreatingTicket
  3. OnRedirectToAuthorizationEndpoint
  4. OnRemoteFailure

は例がありますこれがどこで行われているのですか?私は何かを見つけることができない。

答えて

3

私はCookieミドルウェアで行っています。 ClaimsPrincipalをGoogleにログインさせるために 'temp' Cookieミドルウェアを追加し、次に、本当のCookieミドルウェアにサインインして、豊富なClaimsPrincipalを維持しました。起動クラスの設定メソッドのコードの関連作品:googleOptionsの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 googleOptions = new GoogleOptions() 
    { 
     AuthenticationScheme = "Google", 
     SignInScheme = "Temp", 
     AppId = "yourappidhere", 
     AppSecret = "yourappsecrethere" 
    }; 
    googleOptions.Scope.Add("scopesyouneed"); 

    app.UseGoogleAuthentication(googleOptions); 

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

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> LogInGoogle(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("Google", props)); 

} 

注:

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

var externalPrincipal =待機しているHttpContext.Authentication.AuthenticateAsync( "Temp");

この時点で、クレームで何をする必要があれば何でもできます。私はあなたが見ることができるように電子メールの主張を抽出します。また、CookiePrincipleをクッキーに保持するために、「Cookie」サインイン方式でサインインします。しかし、ユーザーからより多くの情報を要求するフォームを使用してビューにリダイレクトすることもできます。

関連する問題