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 >
@if (User.Identity.IsAuthenticated)
{
<li >
<a href="#" data-bs-toggle="dropdown">
@User.Identity.Name <b ></b>
</a>
<div >
<a asp-route-area="OrchardCore.Admin" asp-controller="Admin" asp-action="Index">
<i ></i>Dashboard
</a>
@if (allowChangeEmail)
{
<a asp-route-area="OrchardCore.Users" asp-controller="ChangeEmail" asp-action="Index">
<i ></i>Change Email
</a>
}
<a asp-route-area="OrchardCore.Users" asp-controller="Account" asp-action="ChangePassword">
<i ></i>Change Password
</a>
@if (externalAuthenticationSchemes.Count() > 0)
{
<a asp-route-area="OrchardCore.Users" asp-controller="Account" asp-action="ExternalLogins">
<i ></i>External Logins
</a>
}
<form asp-route-area="OrchardCore.Users" method="post" asp-controller="Account" asp-action="LogOff">
@*<form asp-route-area="OrchardCore.Users"
method="post" action="/Users/LogOff">*@
<button type="submit" >
<i ></i>Log out
</button>
</form>
</div>
</li>
}
else
{
<li >
<a asp-route-area="OrchardCore.Users" asp-controller="Account" asp-action="Login">
<i ></i>
</a>
</li>
}
</ul>
</div>
CodePudding user response:
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