![](/img/trans.png)
[英]Validate authentication cookie with ASP.NET Core 2.1 / 3+ Identity
[英]ASP.NET Core 2.1 - Authentication cookie deleted but the user could still login without being redirected to external sign-in
再會! 我目前正在創建一個利用 Google 身份驗證來啟用內容個性化的網站。 我在登錄和檢索登錄用戶的信息方面沒有問題,但是當我調用 SignOutAsync() 函數時,.NET 並沒有完全注銷用戶,因為用戶可以在再次單擊登錄按鈕時立即登錄。 清除瀏覽器緩存后,單擊登錄按鈕時,用戶將被重定向到 Google 登錄頁面。
Startup.cs 中的服務配置:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Configure authentication service
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "Google";
})
.AddCookie("Cookies")
.AddGoogle("Google", options =>
{
options.ClientId = Configuration["Authentication:Google:ClientId"];
options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IRecommender, OntologyRecommender>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Startup.cs 中的中間件配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
UserController.cs 中的登錄操作:
public IActionResult Login()
{
return Challenge(new AuthenticationProperties() { RedirectUri = "/" });
}
UserController.cs 中的注銷操作:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
HttpContext.Response.Cookies.Delete(".AspNetCore.Cookies");
return RedirectToAction("Index", "Home");
}
我是 ASP.NET Core 身份驗證領域的新手,所以如果有人能在這個問題上幫助我,我將不勝感激,謝謝!
您需要遍歷應用程序Cookie-這是一個示例代碼片段:
if (HttpContext.Request.Cookies[".MyCookie"] != null)
{
var siteCookies = HttpContext.Request.Cookies.Where(c => c.Key.StartsWith(".MyCookie"));
foreach (var cookie in siteCookies)
{
Response.Cookies.Delete(cookie.Key);
}
}
您可以將用戶重定向到Google的注銷端點以注銷:
await HttpContext.SignOutAsync();
HttpContext.Response.Cookies.Delete(".AspNetCore.Cookies");
return Redirect("https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://localhost:44310");
用您自己的網站url替換“ https:// localhost:44310 ”。 之后,當用戶再次單擊登錄時,用戶將被重定向到Google登錄頁面。
這不是 .NET 問題,而是 Google 的工作方式。 發生這種情況的原因很簡單,因為只有一個帳戶登錄到 Google 帳戶,該過程會默認該帳戶。 從谷歌帳戶注銷您的谷歌帳戶或使用其他帳戶登錄。
不過,請務必從您用於開發的瀏覽器中執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.