2017-07-20 11:21:28 +02:00
|
|
|
|
using System.Net.Http;
|
2015-11-19 18:12:21 +01:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
|
using System.ServiceModel.Channels;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Core.Security;
|
|
|
|
|
|
using Umbraco.Web.WebApi;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Security
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class WebAuthExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2018-03-27 10:51:41 +02:00
|
|
|
|
/// This will set a an authenticated IPrincipal to the current request for webforms & webapi
|
2015-11-19 18:12:21 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
2018-03-27 10:51:41 +02:00
|
|
|
|
/// <param name="principal"></param>
|
2015-11-19 18:12:21 +01:00
|
|
|
|
/// <returns></returns>
|
2018-03-27 10:51:41 +02:00
|
|
|
|
internal static IPrincipal SetPrincipalForRequest(this HttpRequestMessage request, IPrincipal principal)
|
2015-11-19 18:12:21 +01:00
|
|
|
|
{
|
|
|
|
|
|
//It is actually not good enough to set this on the current app Context and the thread, it also needs
|
2017-07-20 11:21:28 +02:00
|
|
|
|
// to be set explicitly on the HttpContext.Current !! This is a strange web api thing that is actually
|
2019-01-26 10:52:19 -05:00
|
|
|
|
// an underlying fault of asp.net not propagating the User correctly.
|
2015-11-19 18:12:21 +01:00
|
|
|
|
if (HttpContext.Current != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
HttpContext.Current.User = principal;
|
|
|
|
|
|
}
|
|
|
|
|
|
var http = request.TryGetHttpContext();
|
|
|
|
|
|
if (http)
|
|
|
|
|
|
{
|
|
|
|
|
|
http.Result.User = principal;
|
|
|
|
|
|
}
|
|
|
|
|
|
Thread.CurrentPrincipal = principal;
|
|
|
|
|
|
|
|
|
|
|
|
//For WebAPI
|
|
|
|
|
|
request.SetUserPrincipal(principal);
|
|
|
|
|
|
|
|
|
|
|
|
return principal;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This will set a an authenticated IPrincipal to the current request given the IUser object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
2018-03-27 10:51:41 +02:00
|
|
|
|
/// <param name="principal"></param>
|
2015-11-19 18:12:21 +01:00
|
|
|
|
/// <returns></returns>
|
2018-03-27 10:51:41 +02:00
|
|
|
|
internal static IPrincipal SetPrincipalForRequest(this HttpContextBase httpContext, IPrincipal principal)
|
|
|
|
|
|
{
|
2015-11-19 18:12:21 +01:00
|
|
|
|
//It is actually not good enough to set this on the current app Context and the thread, it also needs
|
2017-07-20 11:21:28 +02:00
|
|
|
|
// to be set explicitly on the HttpContext.Current !! This is a strange web api thing that is actually
|
2019-01-26 10:52:19 -05:00
|
|
|
|
// an underlying fault of asp.net not propagating the User correctly.
|
2015-11-19 18:12:21 +01:00
|
|
|
|
if (HttpContext.Current != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
HttpContext.Current.User = principal;
|
|
|
|
|
|
}
|
|
|
|
|
|
httpContext.User = principal;
|
|
|
|
|
|
Thread.CurrentPrincipal = principal;
|
|
|
|
|
|
return principal;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|