Updates the startup auth code extension methods to better support extensibility so people could override the default user store or manager in order to implement some interfaces that we currently don't.

This commit is contained in:
Shannon
2015-03-26 17:43:22 +11:00
parent e468492064
commit 6efd14eff3
5 changed files with 97 additions and 48 deletions

View File

@@ -12,7 +12,7 @@ namespace Umbraco.Core.Models.Identity
public class BackOfficeIdentityUser : IdentityUser<int, IIdentityUserLogin, IdentityUserRole<string>, IdentityUserClaim<int>>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(BackOfficeUserManager manager)
public virtual async Task<ClaimsIdentity> GenerateUserIdentityAsync(BackOfficeUserManager manager)
{
// NOTE the authenticationType must match the umbraco one
// defined in CookieAuthenticationOptions.AuthenticationType

View File

@@ -18,6 +18,18 @@ namespace Umbraco.Core.Models.Identity
where TRole : IdentityUserRole<string>
where TClaim : IdentityUserClaim<TKey>
{
/// <summary>
/// Constructor
///
/// </summary>
public IdentityUser()
{
this.Claims = new List<TClaim>();
this.Roles = new List<TRole>();
this.Logins = new List<TLogin>();
}
/// <summary>
/// Email
///
@@ -108,15 +120,6 @@ namespace Umbraco.Core.Models.Identity
/// </summary>
public virtual string UserName { get; set; }
/// <summary>
/// Constructor
///
/// </summary>
public IdentityUser()
{
this.Claims = new List<TClaim>();
this.Roles = new List<TRole>();
this.Logins = new List<TLogin>();
}
}
}

View File

@@ -13,47 +13,14 @@ using Umbraco.Core.Services;
namespace Umbraco.Core.Security
{
/// <summary>
/// Back office user manager
/// Default back office user manager
/// </summary>
public class BackOfficeUserManager : UserManager<BackOfficeIdentityUser, int>
public class BackOfficeUserManager : BackOfficeUserManager<BackOfficeIdentityUser>
{
public BackOfficeUserManager(IUserStore<BackOfficeIdentityUser, int> store)
: base(store)
{
}
#region What we support do not currently
//NOTE: Not sure if we really want/need to ever support this
public override bool SupportsUserClaim
{
get { return false; }
}
//TODO: Support this
public override bool SupportsQueryableUsers
{
get { return false; }
}
//TODO: Support this
public override bool SupportsUserLockout
{
get { return false; }
}
//TODO: Support this
public override bool SupportsUserTwoFactor
{
get { return false; }
}
//TODO: Support this
public override bool SupportsUserPhoneNumber
{
get { return false; }
}
#endregion
/// <summary>
/// Creates a BackOfficeUserManager instance with all default options and the default BackOfficeUserManager
@@ -155,10 +122,51 @@ namespace Umbraco.Core.Security
return manager;
}
}
protected override void Dispose(bool disposing)
/// <summary>
/// Generic Back office user manager
/// </summary>
public class BackOfficeUserManager<T> : UserManager<T, int>
where T : BackOfficeIdentityUser
{
public BackOfficeUserManager(IUserStore<T, int> store)
: base(store)
{
base.Dispose(disposing);
}
#region What we support do not currently
//NOTE: Not sure if we really want/need to ever support this
public override bool SupportsUserClaim
{
get { return false; }
}
//TODO: Support this
public override bool SupportsQueryableUsers
{
get { return false; }
}
//TODO: Support this
public override bool SupportsUserLockout
{
get { return false; }
}
//TODO: Support this
public override bool SupportsUserTwoFactor
{
get { return false; }
}
//TODO: Support this
public override bool SupportsUserPhoneNumber
{
get { return false; }
}
#endregion
}
}

View File

@@ -472,6 +472,12 @@ namespace Umbraco.Web.Editors
var user = await UserManager.FindAsync(loginInfo.Login);
if (user != null)
{
//TODO: It might be worth keeping some of the claims associated with the ExternalLoginInfo, in which case we
// wouldn't necessarily sign the user in here with the standard login, instead we'd update the
// UseUmbracoBackOfficeExternalCookieAuthentication extension method to have the correct provider and claims factory,
// ticket format, etc.. to create our back office user including the claims assigned and in this method we'd just ensure
// that the ticket is created and stored and that the user is logged in.
//sign in
await SignInAsync(user, isPersistent: false);
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
@@ -31,6 +32,9 @@ namespace Umbraco.Web.Security.Identity
ApplicationContext appContext,
MembershipProviderBase userMembershipProvider)
{
if (appContext == null) throw new ArgumentNullException("appContext");
if (userMembershipProvider == null) throw new ArgumentNullException("userMembershipProvider");
//Don't proceed if the app is not ready
if (appContext.IsConfigured == false
|| appContext.DatabaseContext == null
@@ -57,6 +61,10 @@ namespace Umbraco.Web.Security.Identity
MembershipProviderBase userMembershipProvider,
BackOfficeUserStore customUserStore)
{
if (appContext == null) throw new ArgumentNullException("appContext");
if (userMembershipProvider == null) throw new ArgumentNullException("userMembershipProvider");
if (customUserStore == null) throw new ArgumentNullException("customUserStore");
//Don't proceed if the app is not ready
if (appContext.IsConfigured == false
|| appContext.DatabaseContext == null
@@ -70,6 +78,30 @@ namespace Umbraco.Web.Security.Identity
userMembershipProvider));
}
/// <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)
where TManager : BackOfficeUserManager<TUser>
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
if (appContext.IsConfigured == false
|| appContext.DatabaseContext == null
|| appContext.DatabaseContext.IsDatabaseConfigured == false) return;
//Configure Umbraco user manager to be created per request
app.CreatePerOwinContext<TManager>(userManager);
}
/// <summary>
/// Ensures that the UmbracoBackOfficeAuthenticationMiddleware is assigned to the pipeline
/// </summary>