Files
Umbraco-CMS/src/Umbraco.Web.Common/Extensions/HttpContextExtensions.cs

118 lines
3.8 KiB
C#
Raw Normal View History

using System.Security.Claims;
2021-08-06 11:26:31 +02:00
using System.Text;
2021-08-05 21:42:36 +02:00
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Primitives;
using Umbraco.Cms.Core;
namespace Umbraco.Extensions;
public static class HttpContextExtensions
{
/// <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;
2021-08-06 11:26:31 +02:00
if (httpContext.Request.Headers.TryGetValue("Authorization", out StringValues authHeaders))
{
var authHeader = authHeaders.ToString();
if (authHeader is not null && authHeader.StartsWith("Basic"))
2021-08-06 11:26:31 +02:00
{
// Extract credentials.
var encodedUsernamePassword = authHeader.Substring(6).Trim();
Encoding encoding = Encoding.UTF8;
var usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
2021-08-06 11:26:31 +02:00
var seperatorIndex = usernamePassword.IndexOf(':');
2021-08-06 11:26:31 +02:00
username = usernamePassword.Substring(0, seperatorIndex);
password = usernamePassword.Substring(seperatorIndex + 1);
2021-08-06 11:26:31 +02:00
}
return true;
2021-08-06 11:26:31 +02:00
}
return false;
}
2021-08-05 21:42:36 +02:00
/// <summary>
/// Runs the authentication process
/// </summary>
public static async Task<AuthenticateResult> AuthenticateBackOfficeAsync(this HttpContext? httpContext)
{
if (httpContext == null)
{
return AuthenticateResult.NoResult();
}
AuthenticateResult result =
await httpContext.AuthenticateAsync(Constants.Security.BackOfficeAuthenticationType);
if (!result.Succeeded)
{
result =
await httpContext.AuthenticateAsync(Constants.Security.BackOfficeExternalAuthenticationType);
}
return result;
}
/// <summary>
/// Get the value in the request form or query string for the key
/// </summary>
V11/feature/update to dotnet 7 (#12712) * Update projects to .NET 7 * Fix nullability errors * Fix up pipelines to run 7.0 * Update langversion to preview * Revert "Fix up pipelines to run 7.0" This reverts commit d0fa8d01b8126a4eaa59832a3814a567705419ae. * Fix up pipelines again, this time without indentation changes * Include preview versions * Versions not Version * Fix ModelTypeTests * Fix MemberPasswordHasherTests Microsoft wants to use SHA512 instead of SHA256, so our old hashes will return SuccessRehashNeeded now * Use dotnet cli instead of nuget restore * Update src/Umbraco.Web.UI/Umbraco.Web.UI.csproj * Update dependencies * Fix nullability issues * Fix unit test * Fix nullability in ChangingPasswordModel OldPassword can be null, if we're changing the password with password reset enabled. Additionally, we might as well use the new required keyword instead of supressing null. * Use required keyword instead of supressing null * Fix up pipelines again * fix up spelling-error * Use dotnet cli instead of nuget restore * Fix up another NuGet command * Use dotnet version 7 before building * Include preview versions * Remove condition * Use dotnet 7 before running powershell script * Update templates to .net 7 * Download version 7 before running linux container * Move use dotnet 7 even earlier in E2E process * Remove dotnet 7 * Reintroduce .NET 7 task * Update linux docker container and remove dotnet 7 from yml * Fix up dockerfile with ARG * Fix up docker file with nightly builds of dotnet 7 * Reintroduce dotnet 7 so windows can use it * Use aspnet 7 in docker Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
2022-08-23 11:31:05 +02:00
public static string? GetRequestValue(this HttpContext context, string key)
{
HttpRequest request = context.Request;
if (!request.HasFormContentType)
{
return request.Query[key];
}
V11/feature/update to dotnet 7 (#12712) * Update projects to .NET 7 * Fix nullability errors * Fix up pipelines to run 7.0 * Update langversion to preview * Revert "Fix up pipelines to run 7.0" This reverts commit d0fa8d01b8126a4eaa59832a3814a567705419ae. * Fix up pipelines again, this time without indentation changes * Include preview versions * Versions not Version * Fix ModelTypeTests * Fix MemberPasswordHasherTests Microsoft wants to use SHA512 instead of SHA256, so our old hashes will return SuccessRehashNeeded now * Use dotnet cli instead of nuget restore * Update src/Umbraco.Web.UI/Umbraco.Web.UI.csproj * Update dependencies * Fix nullability issues * Fix unit test * Fix nullability in ChangingPasswordModel OldPassword can be null, if we're changing the password with password reset enabled. Additionally, we might as well use the new required keyword instead of supressing null. * Use required keyword instead of supressing null * Fix up pipelines again * fix up spelling-error * Use dotnet cli instead of nuget restore * Fix up another NuGet command * Use dotnet version 7 before building * Include preview versions * Remove condition * Use dotnet 7 before running powershell script * Update templates to .net 7 * Download version 7 before running linux container * Move use dotnet 7 even earlier in E2E process * Remove dotnet 7 * Reintroduce .NET 7 task * Update linux docker container and remove dotnet 7 from yml * Fix up dockerfile with ARG * Fix up docker file with nightly builds of dotnet 7 * Reintroduce dotnet 7 so windows can use it * Use aspnet 7 in docker Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
2022-08-23 11:31:05 +02:00
string? value = request.Form[key];
return value ?? request.Query[key];
}
public static void SetPrincipalForRequest(this HttpContext context, ClaimsPrincipal? principal)
{
if (principal is not null)
{
context.User = principal;
}
}
public static void SetReasonPhrase(this HttpContext httpContext, string? reasonPhrase)
{
// TODO we should update this behavior, as HTTP2 do not have ReasonPhrase. Could as well be returned in body
// https://github.com/aspnet/HttpAbstractions/issues/395
IHttpResponseFeature? httpResponseFeature = httpContext.Features.Get<IHttpResponseFeature>();
if (!(httpResponseFeature is null))
{
httpResponseFeature.ReasonPhrase = reasonPhrase;
}
}
/// <summary>
/// This will return the current back office identity.
/// </summary>
/// <param name="http"></param>
/// <returns>
/// Returns the current back office identity if an admin is authenticated otherwise null
/// </returns>
public static ClaimsIdentity? GetCurrentIdentity(this HttpContext http)
{
// If it's already a UmbracoBackOfficeIdentity
ClaimsIdentity? backOfficeIdentity = http.User.GetUmbracoIdentity();
if (backOfficeIdentity != null)
{
return backOfficeIdentity;
}
return null;
}
}