Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当用户未登录时,如何显示登录链接?_C#_Asp.net Core_.net Core_Blazor_Blazor Server Side - Fatal编程技术网

C# 当用户未登录时,如何显示登录链接?

C# 当用户未登录时,如何显示登录链接?,c#,asp.net-core,.net-core,blazor,blazor-server-side,C#,Asp.net Core,.net Core,Blazor,Blazor Server Side,我正在使用ASP.NET Core Blazor服务器端、.NET 5版本5.0.100-preview.8.20417.9、Microsoft SQLServer 2019数据库、ASP.NET Core Identity、Microsoft Visual Studio Community 2019 preview-版本16.8.0 preview 2.1。我想当没有用户登录时,显示登录链接。当用户登录成功时,显示web应用程序。我的代码如下 文件MainLayout.razor @inher

我正在使用ASP.NET Core Blazor服务器端、.NET 5版本
5.0.100-preview.8.20417.9
、Microsoft SQLServer 2019数据库、ASP.NET Core Identity、Microsoft Visual Studio Community 2019 preview-版本16.8.0 preview 2.1。我想当没有用户登录时,显示登录链接。当用户登录成功时,显示web应用程序。我的代码如下

文件
MainLayout.razor

@inherits LayoutComponentBase
@implements IDisposable

@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
@inject IJSRuntime JSRuntime

@using Microsoft.JSInterop;
<AuthorizeView>
    <Authorized>


<div class="sidebar">
    <div class="top-row logo-container pl-4 navbar-dark bg-light text-body">
        <button class="navbar-toggler" @onclick="@ToggleNavMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
        <span class="logo-image" />
        @*<div class="@($"bg-light text-dark d-inline-block theme-settings { ThemeSwitcherShown }")">
                <a class="nav-item nav-link" @onclick="@ToggleThemeSwitcherPanel" href="javascript:void(0);">
                    <span class="demo-theme-icon"></span>
                </a>
            </div>*@
    </div>
    <div class="@NavMenuCssClass">
        <NavMenu />
    </div>
</div>

<div class="main">
    <div class="top-row bg-light text-body px-4" id="navbar">
        <div class="logo">
            <h5 class="caption">Foo</h5>
        </div>
        <div class="@($"theme-settings { ThemeSwitcherShown }")">
            <a class="nav-item nav-link" @onclick="@ToggleThemeSwitcherPanel" href="javascript:void(0);">
                <span class="demo-theme-icon"></span>
            </a>
        </div>
    </div>
    <div class="content px-4">
        @Body
    </div>
</div>


<ThemeSwitcher @bind-Shown="@ThemeSwitcherShown"></ThemeSwitcher>
    </Authorized>
</AuthorizeView>

@if(!HttpContext.User.Identity.IsAuthenticated) 
{
    <a href="/Identity/Account/Login">Log in</a>
}


@code {
    string NavMenuCssClass { get; set; } = "";

    void ToggleNavMenu()
    {
        NavMenuCssClass = string.IsNullOrEmpty(NavMenuCssClass) || NavMenuCssClass.Contains("d-none") ? "d-block d-xl-none" : "d-none d-xl-flex";
        ThemeSwitcherShown = false;
    }

    bool themeSwitcherShown = false;
    bool ThemeSwitcherShown
    {
        get => themeSwitcherShown;
        set
        {
            themeSwitcherShown = value;
            InvokeAsync(StateHasChanged);
        }
    }

    void ToggleThemeSwitcherPanel()
    {
        ThemeSwitcherShown = !ThemeSwitcherShown;
    }

    string UriFragment { get; set; } = "";

    void OnLocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (!string.IsNullOrEmpty(NavMenuCssClass))
        {
            NavMenuCssClass = "";
            InvokeAsync(StateHasChanged);
        }
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        NavigationManager.LocationChanged += OnLocationChanged;
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            JSRuntime.InvokeAsync<string>("ScrollToTarget");
        }
        return base.OnAfterRenderAsync(firstRender);
    }

    void IDisposable.Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }
}

(帮我修理这个零件)

错误


如何在用户未登录时显示登录链接?

这在模板中的
LoginDisplay.razor
中演示

    <AuthorizeView>
        <Authorized>
             <!-- Show when user log in. -->
        </Authorized>
        <NotAuthorized>
             <a href="/Identity/Account/Login">Đăng nhập</a>
        </NotAuthorized>
    </AuthorizeView>

这在模板的
LoginDisplay.razor
中演示

    <AuthorizeView>
        <Authorized>
             <!-- Show when user log in. -->
        </Authorized>
        <NotAuthorized>
             <a href="/Identity/Account/Login">Đăng nhập</a>
        </NotAuthorized>
    </AuthorizeView>


您是否尝试了
Context
而不是
HttpContext
?还是错误您是否尝试了
Context
而不是
HttpContext
?还是错误
    <AuthorizeView>
        <Authorized>
             <!-- Show when user log in. -->
        </Authorized>
        <NotAuthorized>
             <a href="/Identity/Account/Login">Đăng nhập</a>
        </NotAuthorized>
    </AuthorizeView>