FarmMapsApiClient/FarmmapsApi/HttpMessageHandlers/FarmmapsAuthenticationHandler.cs

24 lines
908 B
C#
Raw Permalink Normal View History

2020-03-24 08:47:08 +00:00
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using IdentityModel;
namespace FarmmapsApi.HttpMessageHandlers
{
public class FarmmapsAuthenticationHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (!OidcConstants.AuthenticationSchemes.AuthorizationHeaderBearer.Equals(request.Headers?.Authorization.Scheme) ||
string.IsNullOrEmpty(request.Headers?.Authorization.Parameter))
2020-03-24 08:47:08 +00:00
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("You must authenticated before using the API")
2020-03-24 08:47:08 +00:00
};
}
return await base.SendAsync(request, cancellationToken);
}
}
}