【问题标题】:ASP.NET Core MVC Google Auth Logout issueASP.NET Core MVC Google Auth 注销问题
【发布时间】:2022-07-26 06:21:11
【问题描述】:

我已经实现了可以使用基本 google auth 登录的代码。用户可以登录,查看显示其电子邮件的页面,然后退出 Google 登录屏幕并选择一个新帐户。

但是,我注意到几天后,由于某种原因,该站点停止要求用户登录,并且该站点自动登录。在这种状态下,用户也无法注销,当使用上面之前工作的原始方法注销时,我仍然可以看到前一个用户的登录信息。我希望用户在每次加载网站时选择登录名,并且我希望用户能够退出而无需进入隐身模式。

其他一些注意事项:

  1. 即使在隐身模式下,如果用户登录,用户也无法退出,直到在此状态下创建一个新的隐身窗口。
  2. 是否有原因登录/注销工作了几天,然后 google chrome(和其他浏览器,如移动 safari)只是变得流氓并停止要求用户登录?
  3. 我也尝试在设置中禁用 chrome 自动登录,但症状仍然存在。
  4. 我尝试切换 UseAuthentication() 和 UseAuthorization() 调用以及其他一些调整,但也许我只是在这里完全出错了。

以下是使用新的 .NET Core 6 MVC Web 应用程序的示例。

Program.cs

using Microsoft.AspNetCore.Authentication.Cookies;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// Using GoogleDefaults.AuthenticationScheme or leaving blank below leads to errors
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie()
    .AddGoogle(options =>
    {
        options.ClientId = "<CLIENT ID FROM GOOGLE CONSOLE>";
        options.ClientSecret = "<SECRET FROM GOOGLE CONSOLE>";
        options.SaveTokens = true;
    });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();
app.UseAuthentication();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

AccountController.cs

public class AccountController : Controller
{
    [AllowAnonymous]
    public IActionResult Login(string redirectUrl)
    {
        return new ChallengeResult("Google");
    }

    [AllowAnonymous]
    public async Task<IActionResult> Logout()
    {
        await HttpContext.SignOutAsync();

        // Redirect to root so that when logging back in, it takes to home page
        return Redirect("/");
    }
}

HomeController.cs

[Authorize(AuthenticationSchemes = GoogleDefaults.AuthenticationScheme)]
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core authentication


    【解决方案1】:

    我自己也遇到过这个。 Google 身份的文档指出,您可以通过 prompt=consent 重定向登录以强制用户选择一个帐户。

    请看这里:https://developers.google.com/identity/protocols/oauth2/openid-connect#re-consent

    Program.cs

    .AddGoogle(options =>
    {
        options.Events.OnRedirectToAuthorizationEndpoint = context =>
        {
            context.Response.Redirect(context.RedirectUri + "&prompt=consent");
            return Task.CompletedTask;
        };
    });
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2019-10-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 2018-08-11
      相关资源
      最近更新 更多