私は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」サインイン方式でサインインします。しかし、ユーザーからより多くの情報を要求するフォームを使用してビューにリダイレクトすることもできます。