简体   繁体   English

ASP.NET身份用户名(如何添加您自己的验证)?

[英]ASP.NET Identity UserName (how to add your own verification)?

Does this property have any verification for itself by default? 默认情况下,此属性是否对其自身进行任何验证? Like Max. 像马克斯 length, etc.? 长度等? If it does, where can it be found? 如果可以,在哪里可以找到?

I'm asking, because i'm intending to override the UserName property to add more verification. 我问,因为我打算重写UserName属性以添加更多验证。 Or should i add the verification in every ViewModel ? 还是应该在每个ViewModel添加验证?

You could just add a Model/ViewModel to handle this for you. 您可以添加一个Model / ViewModel来为您处理。

public class LoginViewModel
{
    [Required(ErrorMessage="*")]
    [StringLength(20, MinimumLength=5)]
    public string UserName { get; set; }

    [DataType(DataType.Password)]
    public string Password { get; set; }
}

This way, in your view, you can notify the user that the length etc does not match. 这样,您便可以通知用户长度等不匹配。 Check out DataAnnotations here 在这里查看DataAnnotations

EDIT Is this what you are trying to do? 编辑这是您要做什么? I have not tested this code! 我尚未测试此代码!

public class ApplicationUser : IdentityUser
{
    public string UserName
    { 
        get; 
        set
        {
            //your validation here
        } 
    }
}

I believe the best way for you to enforce the change is at the core, ie at the model ApplicationUser level. 我认为,实施更改的最佳方法是核心,即模型ApplicationUser级别。

modelBuilder.Entity<ApplicationUser>().Property(t => t.Name).HasMaxLength(10);

This way the database is in sync with your view validations and future controllers are scaffolded correctly. 这样,数据库将与您的视图验证保持同步,并且将来的控制器将被正确放置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM