From 89694cd2b6262707cfb5791c07ae7c0587d38659 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 1 Oct 2026 10:42:04 +0200 Subject: [PATCH] Fix Users UserMenu.cshtml for anonymous visitors and without the Avatar feature - Anonymous visitors get the stock OrchardCore.Users menu (login link) instead of the avatar dropdown. Before, the view built an avatar from a null User.Identity.Name, which throws ArgumentNullException (Parameter 'DisplayName') and returns 500 for every page rendering the user menu (e.g. TheTheme's navbar). - Resolve IAvatarService optionally. Orchard Core binds a module's shape templates to every enabled feature of the module (ShapeTemplateBindingStrategy), so the view is active whenever any OrchardCoreContrib.Users feature is - e.g. only Impersonation - and the [Feature] attribute doesn't prevent that. Without an avatar provider every page threw "No service for type IAvatarService". It now falls back to a user icon, also when the service returns an empty avatar (NullAvatarService). Co-Authored-By: Claude Opus 5.5 --- .../Views/UserMenu.cshtml | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/OrchardCoreContrib.Users/Views/UserMenu.cshtml b/src/OrchardCoreContrib.Users/Views/UserMenu.cshtml index 78813b1..f02b524 100644 --- a/src/OrchardCoreContrib.Users/Views/UserMenu.cshtml +++ b/src/OrchardCoreContrib.Users/Views/UserMenu.cshtml @@ -2,21 +2,68 @@ @using OrchardCore.Users @using OrchardCoreContrib.Avatars @using Microsoft.AspNetCore.Authorization +@using Microsoft.Extensions.DependencyInjection @using System.Security.Claims @inject IAuthorizationService AuthorizationService -@inject IAvatarService AvatarService @attribute [Feature("OrchardCoreContrib.Users.Avatar")] + +@* Same as the stock OrchardCore.Users UserMenu.cshtml for anonymous visitors. *@ +@if (!User.Identity.IsAuthenticated) +{ + @if (Model.AnonymousContent?.Items != null && Model.AnonymousContent.Items.Count > 0) + { + + + return; + } + + + + return; +} + @{ var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); - var avatar = AvatarService.GetAvatar(new AvatarContext { DisplayName = User.Identity.Name }); + // Orchard Core binds a module's shape templates to every enabled feature of the module, so this + // view is active whenever any OrchardCoreContrib.Users feature is (e.g. only Impersonation), not + // just Avatar. IAvatarService comes from the Avatar feature (or another avatar provider, or the + // NullAvatarService fallback of AddOrchardCoreContrib()), so resolve it optionally. + var avatarService = Context.RequestServices.GetService(); + var avatar = avatarService?.GetAvatar(new AvatarContext { DisplayName = User.Identity.Name }); }