【问题标题】:How can I link the AccountModel Password length DataAnnotation with Membership provider minRequiredPasswordLength?如何将 AccountModel 密码长度 DataAnnotation 与成员资格提供程序 minRequiredPasswordLength 相关联?
【发布时间】:2012-02-14 17:10:19
【问题描述】:

使用 asp.net MVC 3 时,从模板安装的默认网站具有 AccountModel。其中,密码属性有DataAnnotations,这里设置了StringLengthMinimumLength

但是,在 .NET 框架成员资格提供程序中,您可以通过 web.config 文件设置 minRequiredPasswordLength

现在如果您将 minRequiredPasswordLength 设置为 6,但将 AccountModel.password MinimumLength 设置为 1,那么如果您的密码长度只有 1 个字符,您将无法注册。反之亦然,minRequiredPasswordLength 为 1 而MinimumLength 为 6 将不允许注册。

所以对我来说这有点倒退。如果应用程序的安装不需要更长的密码长度,那么您必须在模型中进行更改。

DataAnnotations 必须是我所读到的常量值,那么有没有办法将这两者联系起来?理想情况下,我只需要更改 web.config 中的值,而不必担心重建项目。

编辑

在查看更多 Pre MVC 3 工具更新后,我注意到项目模板实际上包含一个 DataAnnotation,它将 Membership minRequiredPasswordLength 属性从 web.config 链接到 AccountModel 在这里写博客:http://timjames.me/mvc-3-password-length-dataannotation

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-membership data-annotations


    【解决方案1】:

    简短的回答是你不能。 DataAnnotations 静态编译到程序集中,不能在运行时更改。

    您可以创建一个查找长度的自定义注释,或者使用远程验证来验证 web.config 中的值。但这不会使用标准的 Length 属性。

    【讨论】:

    • 谢谢。我原以为从一开始就在项目模板中对此有一个优雅的解决方案,因为对我来说,这是 .NET 框架成员资格提供程序的一个非常重要的方面。
    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    相关资源
    最近更新 更多