ASP.NET标识 - 如何在以编程方式创建用户时更改密码要求

时间:2017-03-06 01:21:47

标签: visual-studio-2015 asp.net-core asp.net-identity asp.net-identity-3

我正在使用带有func (node *Node) GetParent() GraphNode { return node.parent } 身份验证的默认ASP.NET Core 1.1模板。 Individual User Accounts上的以下代码抱怨密码要求(例如长度要求,大写,小写等)。我知道我们可以使用VS2015在内置RegisterViewModel上设置这些要求。但由于我在不使用任何ViewModel的情况下以编程方式创建用户,DataAnnotations将无法正常工作。 问题:如何更改密码要求并能够运行以下代码:

DataAnnotations

2 个答案:

答案 0 :(得分:1)

您可以在AddIdentity方法中添加选项......

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>(options =>
        {
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredLength = 1;
            options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.";
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddMvc();

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

答案 1 :(得分:0)

Microsoft Docs网站上有一个最新的教程。

Introduction to Identity on ASP.NET Core

您可以使用的其他设置的示例代码中有更多信息,例如“options.Password”。