【发布时间】:2019-05-14 19:38:05
【问题描述】:
我需要在身份验证成功后使用 OpenID 连接从身份提供者对用户进行身份验证,我需要将用户的声明存储到 cookie 中。
但我在 Request.Cookies 中没有找到接受三个参数的 append 方法,我只能将 keyvaluepair 作为signle 参数传递。
这是我的代码
public IActionResult Login(string provider, string returnUrl = null)
{
Challenge(new AuthenticationProperties { RedirectUri = returnUrl ?? "/" }, provider);
Request.Cookies.Append("key", "value", new CookieOptions());
return View();
}
我还需要确认COOKIE存储的代码应该写在哪里,这样一旦认证成功就不能再次存储了。
这是我的身份验证代码
services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddOpenIdConnect(options =>
{
options.Authority = "https://accounts.google.com";
options.ClientId = _clientID;
options.ResponseType = "code id_token";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.SaveTokens = true;
options.ClientSecret = _clientSecret;
//options.GetClaimsFromUserInfoEndpoint = true;
options.CallbackPath = "/home/index";
//options.SignedOutRedirectUri = redirectUrl;
options.Events = new OpenIdConnectEvents()
{
// handle the logout redirection
OnRedirectToIdentityProviderForSignOut = context =>
{
context.Response.Redirect(redirectUrl);
context.HandleResponse();
return Task.CompletedTask;
}
};
}).AddCookie(options => {
options.LoginPath = "/auth/signin";
});
提前致谢。
【问题讨论】:
标签: openid-connect asp.net-core-2.1 httpcookie