1

我在没有 MVC 但使用 Identity 2.0 的 asp.net 4.5 中重新创建一个项目。网上没有关于在不使用 MVC 的情况下更改身份密码策略的示例……遗憾的是。有谁知道该怎么做?

4

1 回答 1

1

我认为了解这是如何完成的最好方法是通过具有身份验证的模板创建一个新的虚拟网站项目。在 Visual Studio 2013 中,这是由New Project -> Web -> ASP.NET Web Application完成的。在弹出的窗口中选择Web Forms。检查身份验证是否设置为单用户帐户或类似的东西(我在这里使用的是德语版的 Visual Studio)。这应该是默认设置。

身份配置文件

创建项目后,您的解决方案中有很多示例文件。更改为App_Start -> IdentityConfig.cs。在此类中,您可以在此类中设置密码策略:

public class ApplicationUserManager : UserManager<ApplicationUser> {
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store) {}

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

        manager.UserValidator = new UserValidator<ApplicationUser>(manager) {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        manager.PasswordValidator = new PasswordValidator {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser> {
            MessageFormat = "Your security code is: {0}"
        });
        manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser> {
            Subject = "SecurityCode",
            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<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}

在这个街区

manager.PasswordValidator = new PasswordValidator {
    RequiredLength = 6,
    RequireNonLetterOrDigit = true,
    RequireDigit = true,
    RequireLowercase = true,
    RequireUppercase = true,
};

您可以按照自己喜欢的方式配置密码策略。

启动.Auth.cs

然后看看App_Start -> Startup.Auth.cs中的部分类 Startup

在那里您可以看到如何配置身份验证

public partial class Startup {
    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(20),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });
    }
}

启动.cs

最后但并非最不重要的一点是检查您在虚拟项目的根目录中找到的类Startup.cs ,以查看调用ConfigureAuth方法的位置

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(your_dummy_project_namespace.Startup))]
namespace your_dummy_project_namespace
{
    public partial class Startup {
        public void Configuration(IAppBuilder app) {
            ConfigureAuth(app);
        }
    }
}
于 2014-07-22T11:56:03.747 回答