0

In a WASM Blazor application I am injecting the Http client by doing this in the top:

@inject HttpClient Http

Now I want to be able to set a username and password like in regular .NET, there you would do:

handler.Credentials = new NetworkCredential(userName, password);

However, I cannot find any property of Http where I can set my credentials, how can I set it?

ArturM
  • 171
  • 1
  • 11
  • The application I am building is an internal tool and its configuration will be downloaded on page load, including the credentials. – ArturM Feb 12 '20 at 13:09
  • Well, using Jwt would involve changing the API service. The Blazor HttpClient does not seem to support network credentials, and I see no easy way to set the Headers. – H H Feb 12 '20 at 15:11

1 Answers1

1

It's not clear what you do as you provide almost no code to demonstrate your issue. However, it is very clear what you're striving at.

In order to authenticate and authorize users in Blazor WebAssembly App you can use Jwt authentication or OpenID connect that pass a user's credentials to a Web Api end points to verify the users, create a Jwt token, and pass it back to the front end where you can store the Jwt Token in the local storage, and retrieve it when the user logs in, accesses various resources, etc.

Note also that the HttpClient is not really the actual HttpClient. It is based on the JavaScript Fetch Api, and it is missing features like WebSockets, etc.

I've posted in this section answers about how to use Jwt Authentication and OpenID Connection. You'll have to search for those answers dealing with Jwt Authentication, as it was relatively long time ago, and I don't remember their locations. However, here is the links to Adding OpenID Connect to IdentityServer4, and Accessing token from Blazor

Hope this helps...

enet
  • 41,195
  • 5
  • 76
  • 113
  • Seems like what I need is to use Digest authentication in Blazor, is that not possible then? The username and password might be hardcoded in my client and/or downloaded via an configuration file. – ArturM Feb 12 '20 at 13:10
  • I don't think you can use Digest authentication on WebAssembly Blazor App. But you can create a Web Api and use Jwt authentication or OpenID Connect as I've suggested in my answer. – enet Feb 12 '20 at 16:45