我正在使用Identity 2.0在没有MVC的asp.net 4.5中重新创建项目。网上没有关于在不使用MVC的情况下更改身份密码策略的例子......遗憾的是。有谁知道怎么回事?
答案 0 :(得分:1)
我认为了解如何完成此操作的最佳方法是通过模板和身份验证创建一个新的虚拟网站项目。在Visual Studio 2013中,这由 New Project - >完成。网络 - > ASP.NET Web应用程序。在弹出窗口中,选择 Web窗体。检查身份验证是否设置为单个用户帐户或类似的东西(我在这里使用的是德语版的Visual Studio)。这应该是默认设置。
<强> IdentityConfig.cs 强>
创建项目后,解决方案中包含大量示例文件。更改为 App_Start - &gt; 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,
};
您可以按照自己喜欢的方式配置密码策略。
<强> StartUp.Auth.cs 强>
然后看看 App_Start中的部分类Startup - &gt; Startup.Auth.cs
在那里你可以看到,如何配置身份验证
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))
}
});
}
}
<强> Startup.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);
}
}
}