在ASP.NET Core 2中,遇到“注销后重定向不起作用”的问题,可以尝试以下解决方法:
确保注销方法中使用了正确的重定向路径。在注销方法中,使用return SignOut();
进行注销操作,默认情况下会重定向到应用程序的主页。如果需要指定其他路径进行重定向,可以使用return SignOut(new AuthenticationProperties { RedirectUri = "/YourRedirectPath" });
。
确保在Startup.cs文件的ConfigureServices方法中正确配置了身份验证服务。确保AddAuthentication
方法被调用,并且已正确配置了身份验证提供程序和方案。
public void ConfigureServices(IServiceCollection services)
{
// 其他服务配置...
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
});
// 其他服务配置...
}
app.UseAuthentication();
确保身份验证中间件被正确应用。public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// 其他中间件配置...
app.UseAuthentication();
// 其他中间件配置...
}
通过以上步骤的检查和配置,应该可以解决ASP.NET Core 2中的“注销后重定向不起作用”的问题。