ASP.NET Core Razor 页面注销重定向问题:注销时出现白屏

问题描述 投票:0回答:1

我正在使用 Razor Pages 开发 ASP.NET Core 应用程序,并且已经实现了注册和登录功能。但是,我遇到了注销功能的问题。当用户单击注销按钮时,用户应该被重定向到特定页面,但我遇到了白屏问题。

当前的行为如下:单击注销按钮后,浏览器被重定向到“https://localhost:7082/signout-oidc?state=longstring”,在那里我看到一个白屏。我的期望是在注销后将用户重定向到主页或指定页面。

相关代码如下:

Razor 页面中的注销按钮:

<a href='@Url.Action("SignOut", "Account", new { Area = "MicrosoftIdentity" })' class="logout-button">Logout</a>

帐户控制器中的注销操作:

public async Task<IActionResult> SignOut()
{
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    return RedirectToPage("/Index");
}

我怀疑该问题可能与注销过程的处理方式有关,或者可能与注销后的重定向配置不正确有关。注销的重定向设置为“https://localhost:7082/signout-oidc”,但它似乎没有按预期工作。

有人遇到过类似的问题或者可以提供我可能做错了什么的见解吗?如何确保退出过程后,用户被重定向到指定页面(例如主页),而不是留在白屏上?

任何帮助或建议将不胜感激!

c# asp.net asp.net-core razor-pages
1个回答
0
投票

您可以在singoutasync方法中指定signout属性。

        public async Task SignOut()
        {
            var props = new AuthenticationProperties()
            {
                RedirectUri = "/Index"
            };
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme, props);
        }

您还可以在program.cs中设置全局重定向url

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
                {               
                    options.Events.OnSigningOut = (context) =>
                    {
                        context.Response.Redirect("/Index");                                
                        return Task.CompletedTask;
                    };
                });
© www.soinside.com 2019 - 2024. All rights reserved.