Skip to content

Give Blazor SSR access to HTTP Context #50185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
westonwalker opened this issue Aug 18, 2023 · 4 comments
Closed
1 task done

Give Blazor SSR access to HTTP Context #50185

westonwalker opened this issue Aug 18, 2023 · 4 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@westonwalker
Copy link

westonwalker commented Aug 18, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Currently Blazor SSR components do not have access to HTTP Context. So login and register pages have to be created using Razor Pages.

However, if using the Blazor enhanced navigation, switching between Blazor and Razor Page routes causes styling issues due to them using different layouts.

Describe the solution you'd like

I would like to Blazor SSR components to have access to the HTTP Context object. This would solve the above issue and allow us to entirely create apps with authentication in Blazor.

Additional context

No response

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Aug 18, 2023
@marinasundstrom
Copy link

marinasundstrom commented Aug 21, 2023

@westonwalker You should be able to obtain an instance for server-side rendered components by injecting IHttpContextAccessor into your component.

It will of course not work for Server with SignalR since there are no requests, hence no HttpContext.

Program.cs:

builder.Services.AddHttpContextAccessor();

In your component (.razor):

@inject IHttpContextAccessor HttpContextAccessor

Obtain the current HttpContext:

var httpContext = HttpContextAccessor.HttpContext;

@marinasundstrom
Copy link

@westonwalker And I want to add, unless you really want to re-invent everything yourself, you can use what is already in ASP.NET Core to do authentication, and even register users. For Blazor SSR works the same way as with Razor Pages.

There will be official ones, but I myself ported some of the Razor Pages to Razor components:
https://github.com/marinasundstrom/Blazor8Test/tree/main/src/Server/Pages/Authentication

I use cookie authentication both for SSR and interactive client components.

@mkArtakMSFT
Copy link
Contributor

This is a dupe of #48769, which is now done.

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Aug 23, 2023
@ghost ghost added the Status: Resolved label Aug 23, 2023
@westonwalker
Copy link
Author

Thanks for the advice @marinasundstrom . I'll do that until the .NET 8 RC adds Httpcontext access.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants