Viswanatha Swamy 811874b76a
Swamy/remove unused using and refactor the code ()
* Removed Unused Using and Reorganized the Using

* Removed unused using, Reorganized using, moved the class to separate file, removed commented code in Catalog.API

* Revert "Removed unused using, Reorganized using, moved the class to separate file, removed commented code in Catalog.API"

This reverts commit 34241c430619b3b0bbeaabafa44c078c859237c4.

* Removed unused using and reorganized the using inside "Services" folder

* Removed Unused using and reoganized the using

* Refactor Webhooks.API

* Removed unused using and reorganized using inside Catalog.API

* Refactoring

* Removed unsed using

* Added line break just to differentiate between the messages

* Removed unused usings

* Simple Refactoring
2020-12-23 15:19:36 +05:30

36 lines
1.3 KiB
C#

using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace Microsoft.eShopOnContainers.WebMVC.Extensions
{
public static class HttpClientExtensions
{
public static void SetBasicAuthentication(this HttpClient client, string userName, string password) =>
client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(userName, password);
public static void SetToken(this HttpClient client, string scheme, string token) =>
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(scheme, token);
public static void SetBearerToken(this HttpClient client, string token) =>
client.SetToken(JwtConstants.TokenType, token);
}
public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
{
public BasicAuthenticationHeaderValue(string userName, string password)
: base("Basic", EncodeCredential(userName, password))
{ }
private static string EncodeCredential(string userName, string password)
{
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
string credential = String.Format("{0}:{1}", userName, password);
return Convert.ToBase64String(encoding.GetBytes(credential));
}
}
}