在identityserver中使用asp net identity设置自定义用户管理器

时间:2016-12-20 12:47:02

标签: asp.net-mvc asp.net-mvc-5 identityserver3

我使用aspnet identity sample project作为参考。但是,为了进行帐户维护,我需要单独建立客户端。这是因为Brock Allen回答了一个关于更改密码的问题,这个问题应该在一个单独的客户端中进行(最后请参阅https://vimeo.com/154172925)。

所以现在我不确定如何设置用户管理器。在示例项目中,创建了一个新的用户管理器,但没有任何PasswordValidatorlockout设置。我可以在usermanger的构造函数中设置这些选项,以便factory.Register(new Registration<UserManager>());中的UserService将使用它。 但是,当我查看默认的asp net MVC 5应用程序时,public static UserManager Create(IdentityFactoryOptions<UserManager> options, IOwinContext context)方法用于设置PasswordValidatorDataProtectionProvider等。

我不确定如何在没有该功能的情况下设置DataProtectionProvider。因为如果理解正确,则需要进行电子邮件和密码验证。

了解更多信息。这是MVC 5应用程序生成的默认方法:

public static UserManager Create(IdentityFactoryOptions<UserManager> options, IOwinContext context)
{
    var manager = new UserManager(new UserStore(context.Get<Context>()));

    // Configure validation logic for usernames
    manager.UserValidator = new UserValidator<User>(manager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = true
    };

    // Configure validation logic for passwords
    manager.PasswordValidator = new PasswordValidator
    {
        RequiredLength = 6,
        RequireNonLetterOrDigit = true,
        RequireDigit = true,
        RequireLowercase = true,
        RequireUppercase = true,
    };

    // Configure user lockout defaults
    manager.UserLockoutEnabledByDefault = true;
    manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
    manager.MaxFailedAccessAttemptsBeforeLockout = 5;

    // 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 it in here.
    manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<User>
    {
        MessageFormat = "Your security code is {0}"
    });
    manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<User>
    {
        Subject = "Security Code",
        BodyFormat = "Your security code is {0}"
    });
    ////manager.EmailService = new EmailService();
    ////manager.SmsService = new SmsService();
    var dataProtectionProvider = options.DataProtectionProvider;
    if (dataProtectionProvider != null)
    {
        manager.UserTokenProvider =
            new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
    }

    return manager;
}

0 个答案:

没有答案