2.8Adding Authorization Credentials
In the internet society, network security is becoming increasingly critical, especially when integrating with third-party interfaces, which usually requires authentication and authorization before access is granted. Currently, the common authorization methods used by internet application interfaces include Bearer authentication, Basic authentication, Digest authentication, and OAuth authentication.
The following example shows how to add authorization to an HTTP remote request:
// Add Bearer authenticationawait httpRemoteService.SendAsync(HttpRequestBuilder.Get("http://furion.net") .AddBearerAuthentication("your token"));// Add Basic authenticationawait httpRemoteService.SendAsync(HttpRequestBuilder.Get("http://furion.net") .AddBasicAuthentication("username", "password"));// Add Digest authenticationawait httpRemoteService.SendAsync(HttpRequestBuilder.Get("http://furion.net") .AddDigestAuthentication("username", "password"));// Add custom Schema authenticationawait httpRemoteService.SendAsync(HttpRequestBuilder.Get("http://furion.net") .AddAuthentication(new AuthenticationHeaderValue("X-Token", "your token")));If the authorization credentials are correct, the user can successfully access the network resources; otherwise, the service returns a 401 Unauthorized error.
In addition to manually adding authorization credentials for a single request, you can also implement global registration of authorization credentials by creating a custom AuthorizationDelegatingHandler class that inherits from DelegatingHandler:
public class AuthorizationDelegatingHandler : DelegatingHandler{ protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) { // Refer to the SendAsync code return base.Send(request, cancellationToken); } protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // Add Bearer authentication request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "your token"); // Add Basic authentication var base64Credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("username" + ":" + "password")); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials); // Add Digest authentication var digestCredentials = DigestCredentials.GetDigestCredentials($"https://furion.net/digest", "admin", "a123456789", HttpMethod.Get); request.Headers.Authorization = new AuthenticationHeaderValue("Digest", digestCredentials); // Add custom Schema authentication request.Headers.Authorization = new AuthenticationHeaderValue("X-Token", "your token"); return base.SendAsync(request, cancellationToken); }}Note: In practical applications, you should choose one authentication method based on your requirements rather than using multiple authentication headers in a single request. The multiple authentication methods in the code above are only intended to demonstrate how to set different authentication headers.
Next, register AuthorizationDelegatingHandler in the Startup.cs or Program.cs file:
// Register AuthorizationDelegatingHandler as a serviceservices.TryAddSingleton<AuthorizationDelegatingHandler>();// Enable for the default clientservices.AddHttpClient(string.Empty) .AddHttpMessageHandler<AuthorizationDelegatingHandler>();// Enable for a specific client//services.AddHttpClient("weixin")// .AddHttpMessageHandler<AuthorizationDelegatingHandler>()In this way, whenever an HTTP request is sent, it will enter the Send/SendAsync method of the AuthorizationDelegatingHandler class, thereby automatically adding authorization credentials to the request.