|
2 | 2 | using Newtonsoft.Json; |
3 | 3 | using Newtonsoft.Json.Linq; |
4 | 4 | using Skybrud.Essentials.Http.Collections; |
| 5 | +using Skybrud.Essentials.Security; |
5 | 6 | using System.Diagnostics.CodeAnalysis; |
6 | 7 |
|
7 | 8 | namespace Skybrud.Essentials.Http { |
@@ -186,6 +187,35 @@ public static class HttpRequestExtensions { |
186 | 187 | return request; |
187 | 188 | } |
188 | 189 |
|
| 190 | + /// <summary> |
| 191 | + /// Sets the <strong>Authorization</strong> header of <paramref name="request"/> to use <strong>Basic</strong> |
| 192 | + /// authentication based on the specified <paramref name="username"/> and <paramref name="password"/>. |
| 193 | + /// </summary> |
| 194 | + /// <typeparam name="T">The type of the request - eg. <see cref="HttpRequest"/>.</typeparam> |
| 195 | + /// <param name="request">The request.</param> |
| 196 | + /// <param name="username">The username.</param> |
| 197 | + /// <param name="password">The password.</param> |
| 198 | + /// <returns>The specified <paramref name="request"/> as an instance of <typeparamref name="T"/>.</returns> |
| 199 | + [return: NotNullIfNotNull("request")] |
| 200 | + public static T? SetAuthorizationBasic<T>(this T? request, string username, string password) where T : IHttpRequest { |
| 201 | + if (request != null) request.Headers.Authorization = $"Basic {SecurityUtils.Base64Encode($"{username}:{password}")}"; |
| 202 | + return request; |
| 203 | + } |
| 204 | + |
| 205 | + /// <summary> |
| 206 | + /// Sets the <strong>Authorization</strong> header of <paramref name="request"/> using the specified |
| 207 | + /// <strong>Bearer</strong> <paramref name="token"/>. |
| 208 | + /// </summary> |
| 209 | + /// <typeparam name="T">The type of the request - eg. <see cref="HttpRequest"/>.</typeparam> |
| 210 | + /// <param name="request">The request.</param> |
| 211 | + /// <param name="token">The bearer token.</param> |
| 212 | + /// <returns>The specified <paramref name="request"/> as an instance of <typeparamref name="T"/>.</returns> |
| 213 | + [return: NotNullIfNotNull("request")] |
| 214 | + public static T? SetAuthorizationBearer<T>(this T? request, string token) where T : IHttpRequest { |
| 215 | + if (request != null) request.Headers.Authorization = $"Bearer {token}"; |
| 216 | + return request; |
| 217 | + } |
| 218 | + |
189 | 219 | /// <summary> |
190 | 220 | /// Sets the <strong>ContentType</strong> header of <paramref name="request"/>. |
191 | 221 | /// </summary> |
|
0 commit comments