Skip to content

Commit

Permalink
Merge pull request #805 from neozhu/bugfix/httpcontext
Browse files Browse the repository at this point in the history
Remove `IHttpContextAccessor` due to unnecessary use in Blazor Server
  • Loading branch information
neozhu authored Dec 10, 2024
2 parents f1ed953 + 0727540 commit 1fc044e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 75 deletions.
23 changes: 2 additions & 21 deletions src/Server.UI/Components/Shared/Layout/PublicLayout.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
@using Microsoft.AspNetCore.Antiforgery
@layout MainLayout
@layout MainLayout
@inherits LayoutComponentBase
@inject LayoutService LayoutService
@inject IAntiforgery _antiforgery;
@inject IHttpContextAccessor _httpContextAccessor;
@inject IStringLocalizer<SharedResource> L
<MudLayout>
<MudAppBar Elevation="0"
Expand Down Expand Up @@ -37,7 +34,6 @@
<AuthorizeView>
<Authorized>
<form action="@IdentityComponentsEndpointRouteBuilderExtensions.Logout" method="post">
<input type="hidden" name="__RequestVerificationToken" value="@antiforgeryToken" />
<input type="hidden" name="ReturnUrl" value="/"/>
<MudTooltip Text="@L["Logout"]">
<MudButton Style="text-transform:none"
Expand Down Expand Up @@ -66,7 +62,6 @@
<AuthorizeView>
<Authorized>
<form action="@IdentityComponentsEndpointRouteBuilderExtensions.Logout" method="post">
<input type="hidden" name="__RequestVerificationToken" value="@antiforgeryToken" />
<input type="hidden" name="ReturnUrl" value="/" />
<MudTooltip Text="@L["Logout"]">
<MudButton Style="text-transform:none"
Expand Down Expand Up @@ -101,19 +96,5 @@

@code
{
private string? antiforgeryToken;
protected override void OnInitialized()
{
antiforgeryToken = getAntiforgeryToken();
}
private string? getAntiforgeryToken()
{
var httpContext = _httpContextAccessor.HttpContext;
if (httpContext == null)
{
throw new InvalidOperationException("HttpContext is not available.");
}
var tokens = _antiforgery.GetAndStoreTokens(httpContext);
return tokens.RequestToken;
}

}
78 changes: 29 additions & 49 deletions src/Server.UI/Components/Shared/Layout/UserMenu.razor
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
@using Microsoft.AspNetCore.Antiforgery
@inject IStringLocalizer<HeaderMenu> L
@inject IStringLocalizer<HeaderMenu> L
@inject UserProfileStateService UserProfileStateService
@inject IAntiforgery _antiforgery;
@inject IHttpContextAccessor _httpContextAccessor;
@implements IDisposable
<MudTooltip Arrow="true" Text="@L["User Profile"]">
<MudMenu AnchorOrigin="Origin.BottomRight"
Icon="@Icons.Material.Filled.PermIdentity"
Class="mx-1"
LockScroll="true"
TransformOrigin="Origin.TopRight">
Icon="@Icons.Material.Filled.PermIdentity"
Class="mx-1"
LockScroll="true"
TransformOrigin="Origin.TopRight">
<ChildContent>
<div class="pb-4"
style="min-width: 260px;margin-bottom:0px!important">
style="min-width: 260px;margin-bottom:0px!important">
<MudStack Row="true" AlignItems="AlignItems.Center" Class="ma-4">
@if (UserProfile is null || string.IsNullOrEmpty(UserProfile.UserId))
{
<MudProgressCircular Size="Size.Small" Color="Color.Default" Indeterminate="true"/>
<MudProgressCircular Size="Size.Small" Color="Color.Default" Indeterminate="true" />
<MudStack Justify="Justify.Center" Spacing="0">
<MudSkeleton Width="120px"/>
<MudSkeleton Width="120px"/>
<MudSkeleton Width="120px" />
<MudSkeleton Width="120px" />
</MudStack>
}
else
{
<MudAvatar>
@if (string.IsNullOrEmpty(UserProfile.ProfilePictureDataUrl))
{
@(string.IsNullOrEmpty(UserProfile.UserName) ? "" : UserProfile.UserName.First())
}
else
{
<MudImage Src="@UserProfile.ProfilePictureDataUrl"></MudImage>
}
</MudAvatar>

<div class="avatar-container">
<div class="rotating-border"></div>
<MudAvatar Style="width: 40px; height: 40px;">
@if (string.IsNullOrEmpty(UserProfile.ProfilePictureDataUrl))
{
@(string.IsNullOrEmpty(UserProfile.UserName) ? "" : UserProfile.UserName.First())
}
else
{
<MudImage Src="@UserProfile.ProfilePictureDataUrl"></MudImage>
}
</MudAvatar>
</div>
<MudStack Justify="Justify.SpaceBetween" Spacing="0">
<MudText Typo="Typo.body1">@UserProfile.DisplayName</MudText>
<MudText Typo="Typo.body2">@UserProfile.Email</MudText>
</MudStack>
}
</MudStack>

<MudDivider Class="my-2"/>
<MudDivider Class="my-2" />
<MudMenuItem Href="/user/profile">
<div class="d-flex align-center">
<MudIcon Class="mx-2"
Icon="@Icons.Material.Filled.Person"/>
Icon="@Icons.Material.Filled.Person" />
<MudText>@L["Profile"]</MudText>
</div>
</MudMenuItem>
<MudMenuItem OnClick="OnSettingClick">
<div class="d-flex align-center">
<MudIcon Class="mx-2"
Icon="@Icons.Material.Filled.Settings"/>
Icon="@Icons.Material.Filled.Settings" />
<MudText>@L["Settings"]</MudText>
</div>
</MudMenuItem>
<div class="mt-4 mx-4">
<form action="@IdentityComponentsEndpointRouteBuilderExtensions.Logout" method="post">
<input type="hidden" name="__RequestVerificationToken" value="@antiforgeryToken" />
<input type="hidden" name="ReturnUrl" value="/pages/authentication/login" />
<MudButton Color="Color.Secondary"
ButtonType="ButtonType.Submit"
FullWidth="true"
StartIcon="@Icons.Material.Filled.Logout"
Variant="Variant.Outlined">
ButtonType="ButtonType.Submit"
FullWidth="true"
StartIcon="@Icons.Material.Filled.Logout"
Variant="Variant.Outlined">
@L["Logout"]
</MudButton>
</form>
Expand All @@ -80,28 +78,10 @@
{
[Parameter] public EventCallback<MouseEventArgs> OnSettingClick { get; set; }
private UserProfile UserProfile => UserProfileStateService.UserProfile;
private string? antiforgeryToken = string.Empty;
protected override void OnInitialized()
{
UserProfileStateService.OnChange += UserProfileStateService_OnChange;
}
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
antiforgeryToken = getAntiforgeryToken();
}
}
private string? getAntiforgeryToken()
{
var httpContext = _httpContextAccessor.HttpContext;
if (httpContext == null)
{
throw new InvalidOperationException("HttpContext is not available.");
}
var tokens = _antiforgery.GetAndStoreTokens(httpContext);
return tokens.RequestToken;
}
public void Dispose()
{
UserProfileStateService.OnChange -= UserProfileStateService_OnChange;
Expand Down
1 change: 0 additions & 1 deletion src/Server.UI/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public static IServiceCollection AddServerUI(this IServiceCollection services, I
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
});
services.AddScoped<LocalTimeOffset>();
services.AddHttpContextAccessor();
services.AddScoped<HubClient>();
services
.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>()
Expand Down
3 changes: 0 additions & 3 deletions src/Server.UI/Hubs/HubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ namespace CleanArchitecture.Blazor.Server.UI.Hubs;

public sealed class HubClient : IAsyncDisposable
{


private readonly HubConnection _hubConnection;
private bool _started;

public HubClient(NavigationManager navigationManager, IHttpContextAccessor httpContextAccessor)
{
var uri = new UriBuilder(navigationManager.Uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static IEndpointConventionBuilder MapAdditionalIdentityEndpoints(this IEn
await signInManager.SignOutAsync().ConfigureAwait(false);
logger.LogInformation("{UserName} has logged out.", user.Identity?.Name);
return TypedResults.LocalRedirect($"{returnUrl}");
});
}).RequireAuthorization().DisableAntiforgery();

var manageGroup = accountGroup.MapGroup("/Manage").RequireAuthorization();

Expand Down
5 changes: 5 additions & 0 deletions src/Server.UI/wwwroot/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
height: auto !important;
padding: 0rem !important;
}





.blazor-error-boundary {
background: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTYiIGhlaWdodD0iNDkiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIG92ZXJmbG93PSJoaWRkZW4iPjxkZWZzPjxjbGlwUGF0aCBpZD0iY2xpcDAiPjxyZWN0IHg9IjIzNSIgeT0iNTEiIHdpZHRoPSI1NiIgaGVpZ2h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