使用 Identity 混淆基本 Blazor Server 应用程序的注销行为

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

这里是 LoginDisplay.razor,在 /Shared:

<AuthorizeView>
    <Authorized>
        <a href="Identity/Account/Manage">Hello, @context.User.Identity?.Name!</a>
        <form method="POST" action="/Identity/Account/Logout">
            <button type="submit" class="nav-link btn btn-link">Log out</button>
        </form>
    </Authorized>
    <NotAuthorized>
        <a href="Identity/Account/Register">Register</a>
        <a href="Identity/Account/Login">Log in</a>
    </NotAuthorized>
</AuthorizeView>

这里是Logout.cshtml,位于/Areas/Identity/Pages/Account:

@page
@model LogoutModel

@{
    ViewData["Title"] = "Log out";
}

<header>
    <h1>@ViewData["Title"]</h1>
    @{
        if (User.Identity?.IsAuthenticated ?? false)
        {
            <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post">
                <button type="submit" class="nav-link btn btn-link text-dark">Click here to Logout</button>
            </form>
        }
        else
        {
            <p>You have successfully logged out of the application.</p>
        }
    }
</header>

最后是 Logout.cshtml.cs,也位于 /Areas/Identity/Pages/Account:

#nullable disable

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using RecipeAZ.Areas.Identity.Data;

namespace RecipeAZ.Areas.Identity.Pages.Account
{
    public class LogoutModel : PageModel
    {
        private readonly SignInManager<AppUser> _signInManager;
        private readonly ILogger<LogoutModel> _logger;

        public LogoutModel(SignInManager<AppUser> signInManager, ILogger<LogoutModel> logger)
        {
            _signInManager = signInManager;
            _logger = logger;
        }

        public async Task<IActionResult> OnPost(string returnUrl = null)
        {
            Console.WriteLine("log out");
            await _signInManager.SignOutAsync();
            _logger.LogInformation("User logged out.");
            if (returnUrl != null)
            {
                return LocalRedirect(returnUrl);
            }
            else
            {
                // This needs to be a redirect so that the browser performs a new
                // request and the identity for the user gets updated.
                return RedirectToPage();
            }
        }
    }
}

当我在LoginDisplay中点击注销按钮时,它进入一个空白页面,OnPost中的Console.Writeline不执行,OnPost中的断点也不触发。但是,如果我直接输入 url(http://localhost:5000/Identity/Account/Logout),它会显示 Logout.cshtml,我可以从它的按钮成功注销。

如果我将 LoginDisplay 中的表单更改为 get 方法,并将 OnPost 更改为 OnGet,则注销按钮将我注销并重定向到登录页面。但是,从这里单击登录按钮时,我已登录,然后立即注销并再次返回到登录页面。此时显示的地址是“http://localhost:5000/Identity/Account/Login?ReturnUrl=%2FIdentity%2FAccount%2FLogout”,我怀疑这是它立即再次注销我的原因。因此,将其更改为 get 方法毕竟不能解决问题,我的理解是,以这种方式使用 get 方法是不好的做法。

我显然是这里的初学者,所以任何帮助都将不胜感激。

blazor-server-side asp.net-core-identity
© www.soinside.com 2019 - 2024. All rights reserved.