如何将 AccountModel 密码长度 DataAnnotation 与会员提供商 minRequiredPasswordLength 关联?
使用 asp.net MVC 3 时,从模板安装的默认网站具有 AccountModel
。其中,有用于密码属性的 DataAnnotation
,这里设置了 StringLength
的 MinimumLength
。
但是,在 .NET Framework 成员资格提供程序中,您可以通过 web.config
文件设置 minRequiredPasswordLength
。
现在,如果您将 minRequiredPasswordLength
设置为 6,但将 AccountModel.password
MinimumLength
设置为 1,那么您将无法如果您的密码长度只有 1 个字符,请注册。反之亦然,minRequiredPasswordLength
为 1 且 MinimumLength
为 6 将不允许注册。
所以对我来说这有点倒退。如果应用程序的安装不需要更长的密码长度,那么您必须在模型中更改它。
DataAnnotations
必须是我读过的常量值,那么有没有办法链接这两个值?理想情况下,我只想更改 web.config 中的值,而不必担心重建项目。
编辑
在进一步研究 Pre MVC 3 工具更新后,我注意到项目模板实际上包含一个 DataAnnotation,它链接了 web.config 中的 Membership
到 minRequiredPasswordLength
属性AccountModel
在这里发布了有关它的博客: http://timjames.me/mvc-3-password-length-数据注释
When working with asp.net MVC 3, the default website which is installed from the template has an AccountModel
. Within this, there are DataAnnotation
s for the password property and here the MinimumLength
of the StringLength
is set.
However, within the .NET framework Membership provider, you can set the minRequiredPasswordLength
via the web.config
file.
Now if you were to set the minRequiredPasswordLength
to say 6, yet have the AccountModel.password
MinimumLength
set to 1, then you would not be able to register if your password is only 1 character in length. Same the other way around, minRequiredPasswordLength
is 1 and MinimumLength
is 6 will not allow registration.
So to me this is a bit backwards. If an installation of the app does not require the a longer password length, then you have to change this in the model.
DataAnnotations
have to be constant values from what I have read, so is there a way to link these two? Ideally I would like to only have to change the value in the web.config
and not have to worry about rebuilding projects.
Edit
I noticed after looking into this more that Pre MVC 3 Tools Update, the Project Template actually contained a DataAnnotation which linked the Membership minRequiredPasswordLength
property from the web.config
to the AccountModel
Blogged about it here: http://timjames.me/mvc-3-password-length-dataannotation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是你不能。 DataAnnotations 静态编译到程序集中,并且无法在运行时更改。
您可以创建一个查找长度的自定义注释,或者使用远程验证来验证 web.config 中的值。但这不会使用标准的长度属性。
The short answer is that you can't. DataAnnotations are statically compiled into the assembly, and cannot be changed at runtime.
You could create a custom annotation that looked up the length, or use a remote validation to validate against the value in the web.config. But this would not be using the standard Length attribute.