目前我正在尝试让注销在我的 Asp.Net Core 6 Blazor Server 应用程序中正常工作。
登录工作正常。但是注销会重定向到登录 url,并重定向到注销
我将注销 POST 称为以下内容: (这是在 .razor 文件中)
<form method="post" action="Identity/Account/LogOut" asp-area="Identity">
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Outlined" Color="Color.Info">
Ausloggen
</MudButton>
</form>
被调用的url是: https://localhost:7172/Account/Login?ReturnUrl=%2FIdentity%2FAccount%2FLogOut
我没有更改脚手架的 Logout.cshtml 文件。
我正在这样注册我的身份:
builder.Services.AddDefaultIdentity<WaWiIdentityUser>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.User.AllowedUserNameCharacters = "äabcdefghijklmnöopqrstüuvwxyzÄABCDEFGHIJKLMNÖOPQRSTÜUVWXYZ0123456789ß-._@+/ ";
})
.AddRoles<WaWiIdentityRole>()
.AddEntityFrameworkStores<WaWiIdentityContext>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddUserManager<UserManager<WaWiIdentityUser>>()
.AddSignInManager<WaWiSignInManager>()
.AddRoleManager<RoleManager<WaWiIdentityRole>>();
builder.Services.AddAuthentication("Identity.Application").AddCookie();
身份验证 cookie 不会被删除,默认重定向也不会被调用。
这是我使用的默认注销。我尝试记录日志,但据我所知,该方法没有被调用
@functions {
public async Task<IActionResult> OnPost()
{
if (SignInManager.IsSignedIn(User))
{
await SignInManager.SignOutAsync();
}
return Redirect("/");
}
}
我是不是错过了什么?
提前感谢您的帮助。
我遇到了同样的问题。我正在学习教程,相信我已经设置了所有内容......但除了登录页面之外,所有其他脚手架身份页面都会重定向(302)到登录页面。
我看到一种使用 [AllowAnonymous] 属性的解决方案,该属性确实允许访问注销页面(并使用户退出)。将属性应用到其他身份剃刀页面上可以让我访问,但显然,这不是正确的方法。
发现我的程序中缺少以下行。cs:
app.UseAuthorization();
之后我可以访问身份页面(作为登录用户)。我的program.cs 中已经有“app.UseAuthentication();”