2015-02-04 19:24:59 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using Microsoft.Owin;
|
|
|
|
|
|
using Microsoft.Owin.Extensions;
|
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-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-02-04 19:24:59 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Security.Identity
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class AppBuilderExtensions
|
|
|
|
|
|
{
|
2015-02-09 17:37:21 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Configure Identity User Manager for Umbraco
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <param name="appContext"></param>
|
|
|
|
|
|
/// <param name="userMembershipProvider"></param>
|
|
|
|
|
|
public static void ConfigureUserManagerForUmbracoBackOffice(this IAppBuilder app, ApplicationContext appContext, MembershipProviderBase userMembershipProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Don't proceed if the app is not ready
|
|
|
|
|
|
if (appContext.IsConfigured == false
|
|
|
|
|
|
|| appContext.DatabaseContext == null
|
|
|
|
|
|
|| appContext.DatabaseContext.IsDatabaseConfigured == 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(
|
|
|
|
|
|
options,
|
|
|
|
|
|
owinContext,
|
|
|
|
|
|
appContext.Services.UserService,
|
|
|
|
|
|
appContext.Services.ExternalLoginService,
|
|
|
|
|
|
userMembershipProvider));
|
|
|
|
|
|
}
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensures that the UmbracoBackOfficeAuthenticationMiddleware is assigned to the pipeline
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2015-02-06 16:13:02 +11:00
|
|
|
|
public static IAppBuilder UseUmbracoBackOfficeCookieAuthentication(this IAppBuilder app)
|
2015-02-04 19:24:59 +11:00
|
|
|
|
{
|
|
|
|
|
|
if (app == null) throw new ArgumentNullException("app");
|
|
|
|
|
|
|
2015-02-06 14:05:29 +11:00
|
|
|
|
|
|
|
|
|
|
app.UseCookieAuthentication(new UmbracoBackOfficeCookieAuthenticationOptions(
|
2015-02-06 13:47:00 +11:00
|
|
|
|
UmbracoConfig.For.UmbracoSettings().Security,
|
|
|
|
|
|
GlobalSettings.TimeOutInMinutes,
|
2015-02-09 17:37:21 +11:00
|
|
|
|
GlobalSettings.UseSSL)
|
2015-02-06 14:05:29 +11:00
|
|
|
|
{
|
2015-02-06 16:13:02 +11:00
|
|
|
|
Provider = new CookieAuthenticationProvider
|
|
|
|
|
|
{
|
|
|
|
|
|
//// 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<UmbracoMembersUserManager<UmbracoApplicationUser>, UmbracoApplicationUser, int>(
|
|
|
|
|
|
// TimeSpan.FromMinutes(30),
|
|
|
|
|
|
// (manager, user) => user.GenerateUserIdentityAsync(manager),
|
|
|
|
|
|
// identity => identity.GetUserId<int>())
|
|
|
|
|
|
}
|
2015-02-06 14:05:29 +11:00
|
|
|
|
});
|
2015-02-04 19:24:59 +11:00
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-06 16:13:02 +11:00
|
|
|
|
public static IAppBuilder UseUmbracoBackOfficeExternalCookieAuthentication(this IAppBuilder app)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (app == null) throw new ArgumentNullException("app");
|
|
|
|
|
|
|
2015-02-19 16:06:07 +01:00
|
|
|
|
//TODO: Figure out why this isn't working and is only working with the default one, must be a reference somewhere
|
|
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
//app.UseExternalSignInCookie("UmbracoExternalCookie");
|
|
|
|
|
|
|
2015-02-19 16:36:39 +01:00
|
|
|
|
app.SetDefaultSignInAsAuthenticationType("UmbracoExternalCookie");
|
|
|
|
|
|
app.UseCookieAuthentication(new CookieAuthenticationOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
AuthenticationType = Constants.Security.BackOfficeExternalAuthenticationType,
|
|
|
|
|
|
AuthenticationMode = AuthenticationMode.Passive,
|
|
|
|
|
|
CookieName = Constants.Security.BackOfficeExternalAuthenticationType,
|
|
|
|
|
|
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
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//NOTE: This works, but this is just the default implementation which we don't want because other devs
|
|
|
|
|
|
//might want to use this... right?
|
|
|
|
|
|
//app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
|
2015-02-06 16:13:02 +11:00
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-04 19:24:59 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|