Updates based on review feedback

This commit is contained in:
Bjarke Berg
2021-08-06 11:26:31 +02:00
parent aaf28cba21
commit bbd935a0b8
4 changed files with 36 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
@@ -11,6 +12,36 @@ namespace Umbraco.Extensions
{
/// <summary>
/// Try to get the basic auth username and password from the http context.
/// </summary>
public static bool TryGetBasicAuthCredentials(this HttpContext httpContext, out string username, out string password)
{
username = null;
password = null;
if ( httpContext.Request.Headers.TryGetValue("Authorization", out var authHeaders))
{
var authHeader = authHeaders.ToString();
if (authHeader is not null && authHeader.StartsWith("Basic"))
{
//Extract credentials
var encodedUsernamePassword = authHeader.Substring(6).Trim();
var encoding = Encoding.UTF8;
var usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
var seperatorIndex = usernamePassword.IndexOf(':');
username = usernamePassword.Substring(0, seperatorIndex);
password = usernamePassword.Substring(seperatorIndex + 1);
}
return true;
}
return false;
}
/// <summary>
/// Runs the authentication process
/// </summary>