2021-03-04 16:44:09 +11:00
|
|
|
using System;
|
2020-05-25 23:15:32 +10:00
|
|
|
using System.Security.Claims;
|
2021-08-05 21:42:36 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-06-09 13:48:50 +02:00
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
2020-05-25 23:15:32 +10:00
|
|
|
|
|
|
|
|
namespace Umbraco.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class HttpContextExtensions
|
|
|
|
|
{
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await httpContext.AuthenticateAsync(Cms.Core.Constants.Security.BackOfficeAuthenticationType);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 16:44:09 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get the value in the request form or query string for the key
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string GetRequestValue(this HttpContext context, string key)
|
|
|
|
|
{
|
|
|
|
|
HttpRequest request = context.Request;
|
|
|
|
|
if (!request.HasFormContentType)
|
|
|
|
|
{
|
|
|
|
|
return request.Query[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string value = request.Form[key];
|
|
|
|
|
return value ?? request.Query[key];
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 23:15:32 +10:00
|
|
|
public static void SetPrincipalForRequest(this HttpContext context, ClaimsPrincipal principal)
|
|
|
|
|
{
|
|
|
|
|
context.User = principal;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 13:48:50 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
var httpResponseFeature = httpContext.Features.Get<IHttpResponseFeature>();
|
|
|
|
|
if (!(httpResponseFeature is null))
|
|
|
|
|
{
|
|
|
|
|
httpResponseFeature.ReasonPhrase = reasonPhrase;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 23:15:32 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// This will return the current back office identity.
|
|
|
|
|
/// </summary>
|
2020-06-09 13:48:50 +02:00
|
|
|
/// <param name="http"></param>
|
2020-05-25 23:15:32 +10:00
|
|
|
/// <returns>
|
|
|
|
|
/// Returns the current back office identity if an admin is authenticated otherwise null
|
|
|
|
|
/// </returns>
|
2021-02-17 11:50:19 +01:00
|
|
|
public static ClaimsIdentity GetCurrentIdentity(this HttpContext http)
|
2020-05-25 23:15:32 +10:00
|
|
|
{
|
|
|
|
|
if (http == null) throw new ArgumentNullException(nameof(http));
|
|
|
|
|
if (http.User == null) return null; //there's no user at all so no identity
|
|
|
|
|
|
|
|
|
|
// If it's already a UmbracoBackOfficeIdentity
|
|
|
|
|
var backOfficeIdentity = http.User.GetUmbracoIdentity();
|
|
|
|
|
if (backOfficeIdentity != null) return backOfficeIdentity;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|