1

Hello i have an application on which i am doing a custom authentication on the server side. Now i need to port this logic to a Blazor Client.I do not know how i can access HttpContext data such as:

HttpContext.Request.Host.Value
HttpContext.User.Identity.IsAuthenticated

and also how to use the extension method Microsoft.AspNetCore.Authentication.SignInAsync extension method.

How can i get the HttpContext from the client ?

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 1
    Which type of Blazor app? Client-side or Server-side? – H H Jul 08 '19 at 08:38
  • I am interested in both since i am debugging with `Server-Side` but in production it will be `Client-Side`. – Bercovici Adrian Jul 08 '19 at 08:43
  • 2
    https://stackoverflow.com/questions/53817373/how-do-i-access-httpcontext-in-server-side-bazor – Flores Jul 08 '19 at 08:45
  • For authentication, Client- and Server-side are radically different. Even if you could get a context on the Client-side, what do you expect from it? It could be running as a PWA. – H H Jul 08 '19 at 09:02
  • 1
    For me is full explained on [Session #6](https://github.com/dotnet-presentations/blazor-workshop). Using claims on `AuthenticationStateProvider` is the trick. To run in both hosting models, just create a single interface to both auth implementations and use DI. – dani herrera Jul 08 '19 at 09:06

1 Answers1

1

Sending the HttpContext object to the client-side Blazor is a bad idea. HttpContext Accessor is used to inject the HttpContext into classes that have no direct access to the HttpContext object. If you still want to do it, see comment by @ Flores.

I'm not sure what custom authentication system you want to create, but I'd suggest you learn first how to do that the Blazor way. See comment by @dani herrera for a sample by the Blazor team. This sample will also give you the idea of what data you should pass to the client-side Blazor. Of course you can extend this authentication system in many ways: Jwt authentication can be a good exercise for you in extending the Blazor Auth System.

H H
  • 263,252
  • 30
  • 330
  • 514
enet
  • 41,195
  • 5
  • 76
  • 113