2017-07-20 11:21:28 +02:00
using System ;
2018-03-21 09:06:32 +01:00
using System.Web ;
2016-08-12 12:20:00 +02:00
using Microsoft.Owin ;
2018-04-05 23:10:51 +10:00
using Microsoft.Owin.Security ;
2018-08-29 01:15:46 +10:00
using Umbraco.Core ;
2020-01-07 13:50:38 +01:00
using Umbraco.Web.Models.Identity ;
2018-08-29 01:15:46 +10:00
using Umbraco.Web.Security ;
2016-08-12 12:20:00 +02:00
2018-08-29 01:15:46 +10:00
namespace Umbraco.Web
2016-08-12 12:20:00 +02:00
{
public static class OwinExtensions
{
2018-08-29 01:15:46 +10:00
/// <summary>
/// Gets the <see cref="ISecureDataFormat{AuthenticationTicket}"/> for the Umbraco back office cookie
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
internal static ISecureDataFormat < AuthenticationTicket > GetUmbracoAuthTicketDataProtector ( this IOwinContext owinContext )
{
var found = owinContext . Get < UmbracoAuthTicketDataProtector > ( ) ;
return found ? . Protector ;
}
2018-03-21 09:06:32 +01:00
public static string GetCurrentRequestIpAddress ( this IOwinContext owinContext )
{
if ( owinContext = = null )
{
return "Unknown, owinContext is null" ;
}
if ( owinContext . Request = = null )
{
return "Unknown, owinContext.Request is null" ;
}
var httpContext = owinContext . TryGetHttpContext ( ) ;
if ( httpContext = = false )
{
return "Unknown, cannot resolve HttpContext from owinContext" ;
}
return httpContext . Result . GetCurrentRequestIpAddress ( ) ;
}
/// <summary>
2019-01-26 10:52:19 -05:00
/// Nasty little hack to get HttpContextBase from an owin context
2018-03-21 09:06:32 +01:00
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
internal static Attempt < HttpContextBase > TryGetHttpContext ( this IOwinContext owinContext )
{
var ctx = owinContext . Get < HttpContextBase > ( typeof ( HttpContextBase ) . FullName ) ;
return ctx = = null ? Attempt < HttpContextBase > . Fail ( ) : Attempt . Succeed ( ctx ) ;
}
2020-04-02 08:08:09 +01:00
2020-03-13 15:15:14 +00:00
/// <summary>
/// Gets the back office sign in manager out of OWIN
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
2020-04-04 12:59:29 +01:00
public static BackOfficeSignInManager GetBackOfficeSignInManager ( this IOwinContext owinContext )
2020-03-13 15:15:14 +00:00
{
2020-04-04 12:59:29 +01:00
return owinContext . Get < BackOfficeSignInManager > ( )
? ? throw new NullReferenceException ( $"Could not resolve an instance of {typeof (BackOfficeSignInManager)} from the {typeof(IOwinContext)}." ) ;
2020-03-13 15:15:14 +00:00
}
/// <summary>
/// Gets the back office user manager out of OWIN
/// </summary>
/// <param name="owinContext"></param>
/// <returns></returns>
/// <remarks>
/// This is required because to extract the user manager we need to user a custom service since owin only deals in generics and
/// developers could register their own user manager types
/// </remarks>
2020-04-04 12:59:29 +01:00
public static BackOfficeUserManager < BackOfficeIdentityUser > GetBackOfficeUserManager ( this IOwinContext owinContext )
2020-03-13 15:15:14 +00:00
{
2020-04-04 12:59:29 +01:00
var marker = owinContext . Get < IBackOfficeUserManagerMarker > ( BackOfficeUserManager . OwinMarkerKey )
? ? throw new NullReferenceException ( $"No {typeof (IBackOfficeUserManagerMarker)}, i.e. no Umbraco back-office, has been registered with Owin." ) ;
2020-03-13 15:15:14 +00:00
return marker . GetManager ( owinContext )
2020-04-04 12:59:29 +01:00
? ? throw new NullReferenceException ( $"Could not resolve an instance of {typeof (BackOfficeUserManager<BackOfficeIdentityUser>)} from the {typeof (IOwinContext)}." ) ;
2020-03-13 15:15:14 +00:00
}
/// <summary>
/// Adapted from Microsoft.AspNet.Identity.Owin.OwinContextExtensions
/// </summary>
public static T Get < T > ( this IOwinContext context )
{
if ( context = = null ) throw new ArgumentNullException ( nameof ( context ) ) ;
return context . Get < T > ( GetKey ( typeof ( T ) ) ) ;
}
2020-04-20 20:14:36 +01:00
public static IOwinContext Set < T > ( this IOwinContext context , T value )
{
if ( context = = null ) throw new ArgumentNullException ( nameof ( context ) ) ;
return context . Set ( GetKey ( typeof ( T ) ) , value ) ;
}
2020-03-13 15:15:14 +00:00
private static string GetKey ( Type t )
{
return "AspNet.Identity.Owin:" + t . AssemblyQualifiedName ;
}
2020-02-25 18:21:07 +00:00
}
2017-07-20 11:21:28 +02:00
}