CS0618“SignOutSessionStateManager”已过时:“请改用“NavigateToLogout”。将 Blazor WebAssembly 从 .NET 6 升级到 .NET 7 时

Ogg*_*las 6 c# blazor blazor-webassembly .net-7.0

使用个人用户帐户升级 Blazor WebAssembly 时出现以下错误:

错误(活动)CS0618“SignOutSessionStateManager”已过时:“请改用“Microsoft.AspNetCore.Components.Web assembly.Authentication.NavigationManagerExtensions.NavigateToLogout”。

应该如何NavigateToLogout使用?

当前代码:

@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

@inject NavigationManager Navigation
@inject SignOutSessionStateManager SignOutManager

<AuthorizeView>
    <Authorized>
        <a href="authentication/profile">Hello, @context.User.Identity.Name!</a>
        <button class="nav-link btn btn-link" @onclick="BeginSignOut">Log out</button>
    </Authorized>
    <NotAuthorized>
        <a href="authentication/register">Register</a>
        <a href="authentication/login">Log in</a>
    </NotAuthorized>
</AuthorizeView>

@code{
    private async Task BeginSignOut(MouseEventArgs args)
    {
        await SignOutManager.SetSignOutState();
        Navigation.NavigateTo("authentication/logout");
    }
}
Run Code Online (Sandbox Code Playgroud)

Ogg*_*las 10

使用带有个人用户帐户的新 Blazor WebAssembly 项目并查看LoginDisplay.razor它应该像这样使用Navigation.NavigateToLogout("authentication/logout");

完整代码示例:

@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject NavigationManager Navigation

<AuthorizeView>
    <Authorized>
        <a href="authentication/profile">Hello, @context.User.Identity?.Name!</a>
        <button class="nav-link btn btn-link" @onclick="BeginLogOut">Log out</button>
    </Authorized>
    <NotAuthorized>
        <a href="authentication/register">Register</a>
        <a href="authentication/login">Log in</a>
    </NotAuthorized>
</AuthorizeView>

@code{
    private void BeginLogOut()
    {
        Navigation.NavigateToLogout("authentication/logout");
    }
}
Run Code Online (Sandbox Code Playgroud)