2015-02-04 19:24:59 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Web;
|
2015-03-25 10:57:10 +11:00
|
|
|
|
using Microsoft.AspNet.Identity;
|
|
|
|
|
|
using Microsoft.AspNet.Identity.Owin;
|
2015-02-04 19:24:59 +11:00
|
|
|
|
using Microsoft.Owin;
|
|
|
|
|
|
using Microsoft.Owin.Extensions;
|
2015-04-10 14:22:09 +10:00
|
|
|
|
using Microsoft.Owin.Logging;
|
2015-02-19 16:36:39 +01:00
|
|
|
|
using Microsoft.Owin.Security;
|
2015-02-06 14:05:29 +11:00
|
|
|
|
using Microsoft.Owin.Security.Cookies;
|
2015-02-04 19:24:59 +11:00
|
|
|
|
using Owin;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2015-06-18 19:16:49 +02:00
|
|
|
|
using Umbraco.Core.IO;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using Umbraco.Core.Logging;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using Umbraco.Core.Models.Identity;
|
|
|
|
|
|
using Umbraco.Core.Security;
|
2015-03-25 10:57:10 +11:00
|
|
|
|
using Constants = Umbraco.Core.Constants;
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Security.Identity
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class AppBuilderExtensions
|
|
|
|
|
|
{
|
2015-04-10 14:22:09 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the OWIN logger to use Umbraco's logging system
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
public static void SetUmbracoLoggerFactory(this IAppBuilder app)
|
|
|
|
|
|
{
|
|
|
|
|
|
app.SetLoggerFactory(new OwinLoggerFactory());
|
2015-06-18 19:16:49 +02:00
|
|
|
|
}
|
2015-04-10 14:22:09 +10:00
|
|
|
|
|
2015-03-24 12:50:31 +11:00
|
|
|
|
#region Backoffice
|
2015-03-24 13:36:52 +11:00
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
/// <summary>
|
2015-03-24 13:36:52 +11:00
|
|
|
|
/// Configure Default Identity User Manager for Umbraco
|
2015-02-09 17:37:21 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <param name="appContext"></param>
|
|
|
|
|
|
/// <param name="userMembershipProvider"></param>
|
2015-06-18 19:16:49 +02:00
|
|
|
|
public static void ConfigureUserManagerForUmbracoBackOffice(this IAppBuilder app,
|
|
|
|
|
|
ApplicationContext appContext,
|
2015-03-24 13:16:32 +11:00
|
|
|
|
MembershipProviderBase userMembershipProvider)
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2015-03-26 17:43:22 +11:00
|
|
|
|
if (appContext == null) throw new ArgumentNullException("appContext");
|
|
|
|
|
|
if (userMembershipProvider == null) throw new ArgumentNullException("userMembershipProvider");
|
|
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
//Don't proceed if the app is not ready
|
2015-04-01 16:12:32 +11:00
|
|
|
|
if (appContext.IsUpgrading == false && appContext.IsConfigured == false) return;
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
//Configure Umbraco user manager to be created per request
|
|
|
|
|
|
app.CreatePerOwinContext<BackOfficeUserManager>(
|
|
|
|
|
|
(options, owinContext) => BackOfficeUserManager.Create(
|
2015-03-24 12:50:31 +11:00
|
|
|
|
options,
|
2015-02-09 17:37:21 +11:00
|
|
|
|
appContext.Services.UserService,
|
|
|
|
|
|
appContext.Services.ExternalLoginService,
|
|
|
|
|
|
userMembershipProvider));
|
2015-07-01 17:07:29 +02:00
|
|
|
|
|
|
|
|
|
|
//Create a sign in manager per request
|
2015-07-23 12:03:50 +02:00
|
|
|
|
app.CreatePerOwinContext<BackOfficeSignInManager>((options, context) => BackOfficeSignInManager.Create(options, context, app.CreateLogger<BackOfficeSignInManager>()));
|
2015-02-09 17:37:21 +11:00
|
|
|
|
}
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
2015-03-24 13:36:52 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Configure a custom UserStore with the Identity User Manager for Umbraco
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <param name="appContext"></param>
|
|
|
|
|
|
/// <param name="userMembershipProvider"></param>
|
|
|
|
|
|
/// <param name="customUserStore"></param>
|
|
|
|
|
|
public static void ConfigureUserManagerForUmbracoBackOffice(this IAppBuilder app,
|
|
|
|
|
|
ApplicationContext appContext,
|
|
|
|
|
|
MembershipProviderBase userMembershipProvider,
|
|
|
|
|
|
BackOfficeUserStore customUserStore)
|
|
|
|
|
|
{
|
2015-03-26 17:43:22 +11:00
|
|
|
|
if (appContext == null) throw new ArgumentNullException("appContext");
|
|
|
|
|
|
if (userMembershipProvider == null) throw new ArgumentNullException("userMembershipProvider");
|
|
|
|
|
|
if (customUserStore == null) throw new ArgumentNullException("customUserStore");
|
|
|
|
|
|
|
2015-03-24 13:36:52 +11:00
|
|
|
|
//Don't proceed if the app is not ready
|
2015-04-01 16:12:32 +11:00
|
|
|
|
if (appContext.IsUpgrading == false && appContext.IsConfigured == false) return;
|
2015-03-24 13:36:52 +11:00
|
|
|
|
|
|
|
|
|
|
//Configure Umbraco user manager to be created per request
|
|
|
|
|
|
app.CreatePerOwinContext<BackOfficeUserManager>(
|
|
|
|
|
|
(options, owinContext) => BackOfficeUserManager.Create(
|
|
|
|
|
|
options,
|
|
|
|
|
|
customUserStore,
|
|
|
|
|
|
userMembershipProvider));
|
2015-07-01 17:07:29 +02:00
|
|
|
|
|
|
|
|
|
|
//Create a sign in manager per request
|
2015-07-23 12:03:50 +02:00
|
|
|
|
app.CreatePerOwinContext<BackOfficeSignInManager>((options, context) => BackOfficeSignInManager.Create(options, context, app.CreateLogger(typeof(BackOfficeSignInManager).FullName)));
|
2015-03-24 13:36:52 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-03-26 17:43:22 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Configure a custom BackOfficeUserManager for Umbraco
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <param name="appContext"></param>
|
|
|
|
|
|
/// <param name="userManager"></param>
|
|
|
|
|
|
public static void ConfigureUserManagerForUmbracoBackOffice<TManager, TUser>(this IAppBuilder app,
|
|
|
|
|
|
ApplicationContext appContext,
|
|
|
|
|
|
Func<IdentityFactoryOptions<TManager>, IOwinContext, TManager> userManager)
|
2015-06-18 19:16:49 +02:00
|
|
|
|
where TManager : BackOfficeUserManager<TUser>
|
2015-03-26 17:43:22 +11:00
|
|
|
|
where TUser : BackOfficeIdentityUser
|
|
|
|
|
|
{
|
|
|
|
|
|
if (appContext == null) throw new ArgumentNullException("appContext");
|
|
|
|
|
|
if (userManager == null) throw new ArgumentNullException("userManager");
|
|
|
|
|
|
|
|
|
|
|
|
//Don't proceed if the app is not ready
|
2015-04-01 16:12:32 +11:00
|
|
|
|
if (appContext.IsUpgrading == false && appContext.IsConfigured == false) return;
|
2015-03-26 17:43:22 +11:00
|
|
|
|
|
|
|
|
|
|
//Configure Umbraco user manager to be created per request
|
|
|
|
|
|
app.CreatePerOwinContext<TManager>(userManager);
|
2015-07-01 17:07:29 +02:00
|
|
|
|
|
|
|
|
|
|
//Create a sign in manager per request
|
2015-07-23 12:03:50 +02:00
|
|
|
|
app.CreatePerOwinContext<BackOfficeSignInManager>((options, context) => BackOfficeSignInManager.Create(options, context, app.CreateLogger(typeof(BackOfficeSignInManager).FullName)));
|
2015-03-26 17:43:22 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-04 19:24:59 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensures that the UmbracoBackOfficeAuthenticationMiddleware is assigned to the pipeline
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
2015-06-09 12:17:45 +02:00
|
|
|
|
/// <param name="appContext"></param>
|
2015-02-04 19:24:59 +11:00
|
|
|
|
/// <returns></returns>
|
2015-06-09 12:17:45 +02:00
|
|
|
|
public static IAppBuilder UseUmbracoBackOfficeCookieAuthentication(this IAppBuilder app, ApplicationContext appContext)
|
2015-02-04 19:24:59 +11:00
|
|
|
|
{
|
|
|
|
|
|
if (app == null) throw new ArgumentNullException("app");
|
2015-06-09 12:17:45 +02:00
|
|
|
|
if (appContext == null) throw new ArgumentNullException("appContext");
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
2015-06-09 12:17:45 +02:00
|
|
|
|
//Don't proceed if the app is not ready
|
|
|
|
|
|
if (appContext.IsUpgrading == false && appContext.IsConfigured == false) return app;
|
2015-02-06 14:05:29 +11:00
|
|
|
|
|
2015-06-18 19:16:49 +02:00
|
|
|
|
var authOptions = new UmbracoBackOfficeCookieAuthOptions(
|
|
|
|
|
|
UmbracoConfig.For.UmbracoSettings().Security,
|
|
|
|
|
|
GlobalSettings.TimeOutInMinutes,
|
|
|
|
|
|
GlobalSettings.UseSSL)
|
2015-02-06 14:05:29 +11:00
|
|
|
|
{
|
2015-09-16 15:22:40 +02:00
|
|
|
|
Provider = new BackOfficeCookieAuthenticationProvider
|
2015-06-18 19:16:49 +02:00
|
|
|
|
{
|
2015-03-25 10:57:10 +11:00
|
|
|
|
// Enables the application to validate the security stamp when the user
|
|
|
|
|
|
// logs in. This is a security feature which is used when you
|
|
|
|
|
|
// change a password or add an external login to your account.
|
|
|
|
|
|
OnValidateIdentity = SecurityStampValidator
|
|
|
|
|
|
.OnValidateIdentity<BackOfficeUserManager, BackOfficeIdentityUser, int>(
|
|
|
|
|
|
TimeSpan.FromMinutes(30),
|
|
|
|
|
|
(manager, user) => user.GenerateUserIdentityAsync(manager),
|
2015-09-16 15:22:40 +02:00
|
|
|
|
identity => identity.GetUserId<int>()),
|
2015-02-06 16:13:02 +11:00
|
|
|
|
}
|
2015-06-18 19:16:49 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//This is a custom middleware, we need to return the user's remaining logged in seconds
|
2015-07-23 12:03:50 +02:00
|
|
|
|
app.Use<GetUserSecondsMiddleWare>(
|
|
|
|
|
|
authOptions,
|
|
|
|
|
|
UmbracoConfig.For.UmbracoSettings().Security,
|
|
|
|
|
|
app.CreateLogger<GetUserSecondsMiddleWare>());
|
2015-06-18 19:16:49 +02:00
|
|
|
|
|
|
|
|
|
|
app.UseCookieAuthentication(authOptions);
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-22 15:10:14 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensures that the cookie middleware for validating external logins is assigned to the pipeline with the correct
|
|
|
|
|
|
/// Umbraco back office configuration
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
2015-06-09 12:17:45 +02:00
|
|
|
|
/// <param name="appContext"></param>
|
2015-02-22 15:10:14 +01:00
|
|
|
|
/// <returns></returns>
|
2015-06-09 12:17:45 +02:00
|
|
|
|
public static IAppBuilder UseUmbracoBackOfficeExternalCookieAuthentication(this IAppBuilder app, ApplicationContext appContext)
|
2015-02-06 16:13:02 +11:00
|
|
|
|
{
|
|
|
|
|
|
if (app == null) throw new ArgumentNullException("app");
|
2015-06-09 12:17:45 +02:00
|
|
|
|
if (appContext == null) throw new ArgumentNullException("appContext");
|
|
|
|
|
|
|
|
|
|
|
|
//Don't proceed if the app is not ready
|
|
|
|
|
|
if (appContext.IsUpgrading == false && appContext.IsConfigured == false) return app;
|
2015-02-06 16:13:02 +11:00
|
|
|
|
|
2015-02-19 16:36:39 +01:00
|
|
|
|
app.UseCookieAuthentication(new CookieAuthenticationOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
AuthenticationType = Constants.Security.BackOfficeExternalAuthenticationType,
|
|
|
|
|
|
AuthenticationMode = AuthenticationMode.Passive,
|
2015-03-25 12:21:41 +11:00
|
|
|
|
CookieName = Constants.Security.BackOfficeExternalCookieName,
|
2015-02-19 16:36:39 +01:00
|
|
|
|
ExpireTimeSpan = TimeSpan.FromMinutes(5),
|
|
|
|
|
|
//Custom cookie manager so we can filter requests
|
|
|
|
|
|
CookieManager = new BackOfficeCookieManager(new SingletonUmbracoContextAccessor()),
|
|
|
|
|
|
CookiePath = "/",
|
|
|
|
|
|
CookieSecure = GlobalSettings.UseSSL ? CookieSecureOption.Always : CookieSecureOption.SameAsRequest,
|
|
|
|
|
|
CookieHttpOnly = true,
|
|
|
|
|
|
CookieDomain = UmbracoConfig.For.UmbracoSettings().Security.AuthCookieDomain
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2015-02-06 16:13:02 +11:00
|
|
|
|
return app;
|
2015-06-18 19:16:49 +02:00
|
|
|
|
}
|
2015-03-24 12:50:31 +11:00
|
|
|
|
#endregion
|
2015-02-06 16:13:02 +11:00
|
|
|
|
|
2015-02-04 19:24:59 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|