asp.netasp.net-mvcasp.net-coreorchardcmsorchardcore

OrchardCore: Profile menu does not change after publish


I have a menu that I want to toggle when the user is authenticated and when not authenticated. So when authenticated, I want to show the drop down menu that includes username and submenus such as changing password, dashboard, and log out.

In debug mode, it works fine, the profile menu toggles, but after I publish the profile menu does not work. Somehow the value of User.Identity.IsAuthenticated is not updating when authentication status changes.

Menu.cshtml:

@using Microsoft.AspNetCore.Identity
@using Microsoft.Extensions.Options
@using OrchardCore.Admin
@using OrchardCore.Entities
@using OrchardCore.Settings
@using OrchardCore.Users
@using OrchardCore.Users.Models
@inject ISiteService SiteService
@inject SignInManager<IUser> SignInManager
@inject IOptions<AdminOptions> AdminOptions
@using OrchardCore.DisplayManagement.Razor
@inherits OrchardCore.DisplayManagement.Razor.RazorPage<TModel>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    var allowChangeEmail = (await SiteService.GetSiteSettingsAsync()).As<ChangeEmailSettings>().AllowChangeEmail;
    var externalAuthenticationSchemes = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();
    var userCanRegister = (await SiteService.GetSiteSettingsAsync()).As<RegistrationSettings>().UsersCanRegister == UserRegistrationType.AllowRegistration;
}

<ul class="navbar-nav">
    @if (User.Identity.IsAuthenticated)
    {
        <li class="nav-item dropdown">
            <a href="#" class="nav-link px-2 px-lg-3 rounded js-scroll-trigger dropdown-toggle" data-bs-toggle="dropdown">
                @User.Identity.Name <b class="caret"></b>
            </a>
            <div class="dropdown-menu">
                <a class="dropdown-item bottom-line-hover" asp-route-area="OrchardCore.Admin" asp-controller="Admin" asp-action="Index">
                    <i class="fas fa-desktop fa-fw mx-1"></i>Dashboard
                </a>
                @if (allowChangeEmail)
                {
                    <a class="dropdown-item bottom-line-hover" asp-route-area="OrchardCore.Users" asp-controller="ChangeEmail" asp-action="Index">
                        <i class="fas fa-envelope fa-fw mx-1"></i>Change Email
                    </a>
                }

                <a class="dropdown-item bottom-line-hover" asp-route-area="OrchardCore.Users" asp-controller="Account" asp-action="ChangePassword">
                    <i class="fas fa-lock fa-fw mx-1"></i>Change Password
                </a>

                @if (externalAuthenticationSchemes.Count() > 0)
                {
                    <a class="dropdown-item bottom-line-hover" asp-route-area="OrchardCore.Users" asp-controller="Account" asp-action="ExternalLogins">
                        <i class="fas fa-user-lock fa-fw mx-1"></i>External Logins
                    </a>
                }
                <form class="form-inline" asp-route-area="OrchardCore.Users" method="post" asp-controller="Account" asp-action="LogOff">
                    @*<form class="form-inline" asp-route-area="OrchardCore.Users"
                method="post" action="/Users/LogOff">*@
                    <button type="submit" class="dropdown-item bottom-line-hover">
                        <i class="button-text-color fas fa-sign-out-alt fa-fw mx-1"></i>Log out
                    </button>
                </form>
            </div>
        </li>
    }
    else
    {
        <li class="nav-item">
            <a class="nav-link px-2 px-lg-3 rounded js-scroll-trigger" asp-route-area="OrchardCore.Users" asp-controller="Account" asp-action="Login">
                <i class="fas fa-sign-in-alt"></i>
            </a>
        </li>
    }
</ul>

</div>


Solution

  • You can use the implementation of the user menu in the default theme, TheTheme. Check out the Layout.cshtml and the LoginMenu.cshtml to see how it is implemented.

    The source code of the default theme can be located at https://github.com/OrchardCMS/OrchardCore/tree/main/src/OrchardCore.Themes/TheTheme