2015-02-09 17:37:21 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Web.Security;
|
|
|
|
|
|
using Microsoft.AspNet.Identity;
|
|
|
|
|
|
using Microsoft.AspNet.Identity.Owin;
|
|
|
|
|
|
using Microsoft.Owin;
|
|
|
|
|
|
using Umbraco.Core.Models.Identity;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Security
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2015-03-26 17:43:22 +11:00
|
|
|
|
/// Default back office user manager
|
2015-02-09 17:37:21 +11:00
|
|
|
|
/// </summary>
|
2015-03-26 17:43:22 +11:00
|
|
|
|
public class BackOfficeUserManager : BackOfficeUserManager<BackOfficeIdentityUser>
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
|
|
|
|
|
public BackOfficeUserManager(IUserStore<BackOfficeIdentityUser, int> store)
|
|
|
|
|
|
: base(store)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-03-24 13:36:52 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a BackOfficeUserManager instance with all default options and the default BackOfficeUserManager
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <param name="userService"></param>
|
|
|
|
|
|
/// <param name="externalLoginService"></param>
|
|
|
|
|
|
/// <param name="membershipProvider"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2015-02-09 17:37:21 +11:00
|
|
|
|
public static BackOfficeUserManager Create(
|
|
|
|
|
|
IdentityFactoryOptions<BackOfficeUserManager> options,
|
|
|
|
|
|
IUserService userService,
|
|
|
|
|
|
IExternalLoginService externalLoginService,
|
|
|
|
|
|
MembershipProviderBase membershipProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (options == null) throw new ArgumentNullException("options");
|
|
|
|
|
|
if (userService == null) throw new ArgumentNullException("userService");
|
|
|
|
|
|
if (externalLoginService == null) throw new ArgumentNullException("externalLoginService");
|
|
|
|
|
|
|
|
|
|
|
|
var manager = new BackOfficeUserManager(new BackOfficeUserStore(userService, externalLoginService, membershipProvider));
|
|
|
|
|
|
|
2015-03-24 13:36:52 +11:00
|
|
|
|
return InitUserManager(manager, membershipProvider, options);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a BackOfficeUserManager instance with all default options and a custom BackOfficeUserManager instance
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <param name="customUserStore"></param>
|
|
|
|
|
|
/// <param name="membershipProvider"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static BackOfficeUserManager Create(
|
|
|
|
|
|
IdentityFactoryOptions<BackOfficeUserManager> options,
|
|
|
|
|
|
BackOfficeUserStore customUserStore,
|
|
|
|
|
|
MembershipProviderBase membershipProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (options == null) throw new ArgumentNullException("options");
|
|
|
|
|
|
if (customUserStore == null) throw new ArgumentNullException("customUserStore");
|
|
|
|
|
|
|
|
|
|
|
|
var manager = new BackOfficeUserManager(customUserStore);
|
|
|
|
|
|
|
|
|
|
|
|
return InitUserManager(manager, membershipProvider, options);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes the user manager with the correct options
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="manager"></param>
|
|
|
|
|
|
/// <param name="membershipProvider"></param>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static BackOfficeUserManager InitUserManager(BackOfficeUserManager manager, MembershipProviderBase membershipProvider, IdentityFactoryOptions<BackOfficeUserManager> options)
|
|
|
|
|
|
{
|
2015-02-09 17:37:21 +11:00
|
|
|
|
// Configure validation logic for usernames
|
|
|
|
|
|
manager.UserValidator = new UserValidator<BackOfficeIdentityUser, int>(manager)
|
|
|
|
|
|
{
|
|
|
|
|
|
AllowOnlyAlphanumericUserNames = false,
|
|
|
|
|
|
RequireUniqueEmail = true
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Configure validation logic for passwords
|
|
|
|
|
|
manager.PasswordValidator = new PasswordValidator
|
|
|
|
|
|
{
|
|
|
|
|
|
RequiredLength = membershipProvider.MinRequiredPasswordLength,
|
|
|
|
|
|
RequireNonLetterOrDigit = membershipProvider.MinRequiredNonAlphanumericCharacters > 0,
|
|
|
|
|
|
RequireDigit = false,
|
|
|
|
|
|
RequireLowercase = false,
|
|
|
|
|
|
RequireUppercase = false
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//use a custom hasher based on our membership provider
|
|
|
|
|
|
manager.PasswordHasher = new MembershipPasswordHasher(membershipProvider);
|
|
|
|
|
|
|
|
|
|
|
|
var dataProtectionProvider = options.DataProtectionProvider;
|
|
|
|
|
|
if (dataProtectionProvider != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
manager.UserTokenProvider = new DataProtectorTokenProvider<BackOfficeIdentityUser, int>(dataProtectionProvider.Create("ASP.NET Identity"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//custom identity factory for creating the identity object for which we auth against in the back office
|
2015-03-24 13:36:52 +11:00
|
|
|
|
manager.ClaimsIdentityFactory = new BackOfficeClaimsIdentityFactory();
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2015-03-24 13:13:06 +11:00
|
|
|
|
//NOTE: Not implementing these, if people need custom 2 factor auth, they'll need to implement their own UserStore to suport it
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
|
|
|
|
|
//// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
|
|
|
|
|
|
//// You can write your own provider and plug in here.
|
|
|
|
|
|
//manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser>
|
|
|
|
|
|
//{
|
|
|
|
|
|
// MessageFormat = "Your security code is: {0}"
|
|
|
|
|
|
//});
|
|
|
|
|
|
//manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser>
|
|
|
|
|
|
//{
|
|
|
|
|
|
// Subject = "Security Code",
|
|
|
|
|
|
// BodyFormat = "Your security code is: {0}"
|
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
|
|
//manager.EmailService = new EmailService();
|
|
|
|
|
|
//manager.SmsService = new SmsService();
|
|
|
|
|
|
|
|
|
|
|
|
return manager;
|
|
|
|
|
|
}
|
2015-03-26 17:43:22 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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; }
|
|
|
|
|
|
}
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2015-03-26 17:43:22 +11:00
|
|
|
|
//TODO: Support this
|
|
|
|
|
|
public override bool SupportsQueryableUsers
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2015-03-26 17:43:22 +11:00
|
|
|
|
get { return false; }
|
2015-02-09 17:37:21 +11:00
|
|
|
|
}
|
2015-03-26 17:43:22 +11:00
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|