services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToLogin = context =>
{
if (context.Request.Path.StartsWithSegments("/api") && context.Response.StatusCode == 200)
{
context.Response.StatusCode = 401;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.CompletedTask;
};
});
[HttpGet]
[AllowAnonymous]
public async Task Login(string returnUrl = null)
{
await HttpContext.SignOutAsync();
return RedirectToPage("./GoogleAuthenticator", new { returnUrl });
}
public async void OnGet()
{
if (HttpContext.Session.GetString("FirstTimeLogin") == null)
{
HttpContext.Session.SetString("FirstTimeLogin", "true");
Response.Redirect("./FirstTimeQuestion");
}
}
重要提示:以上示例代码中,RedirectToPage和Response.Redirect的导航路径应根据实际需要进行修改。