如何:基于用户角色所需的验证器 ASP.Net MVC 3
我有一个表单,其中有“实际成本”字段,我想自定义其外观,并应根据用户角色对其进行验证。
更清楚地说,客户希望在表单或详细信息页面中显示他的字段,并使其可供角色“高级销售、经理”的用户编辑,但不能编辑其他角色,所以任何人都可以指导我最好的方法吗?
我应该根据角色中的用户编写自定义所需的验证吗?如果可以,您能否提供正确的实现?
有些人可能会告诉我为此创建自定义模型,但我认为这会很麻烦,而且角色将是动态的,因此它不是预定义的角色集。
我希望我说得足够清楚
i have a form where i have the field "Real Cost" i want to customize its appearance and wither it should be validated based on user role.
to be more clear is say the client want to show his field in the form or details page and also make it editable for users in Roles "Senior Sales, Manager" but not other roles, so can anyone please guide me of the best way ?
should i write custom required validation based on user in role, and if so can you please provide the right implementation of it?
some may tell me create custom model for this, but i think it would be hassle plus the Roles will be dynamic so it is not predefined set of roles.
i hope i was clear enough
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
安全性绝对应该发生在模型或控制器中,但绝不应该发生在视图中——这远远超出了视图的关注范围。也就是显示控制器给它的数据。
Security is definitely something that should be happening in the model or the controller but never in the View -- that is well beyond the View's scope of concern. Which is to display the data that the controller gives it.
要扩展@Wyatt,您需要在模型级别做出所有这些决策,然后用所有答案填充“视图模型”,然后可以在视图中使用这些答案来改善用户体验。
在此表单的 ViewModel 中,有一个属性 IsRealCostEditable,该属性将由您的服务/模型层通过检查用户的角色来设置。现在您可以轻松调整该字段的 UI。
To expand on @Wyatt you need to make all these decisions at the model level and then populate a 'View Model' with all the answers, which then can be used in the view to improve user experience.
In the ViewModel for this form, have a property IsRealCostEditable, which will be set by your service/model layer by checking the user's role. Now you can easily adjust the UI for that field.
您可以创建重复的页面,一个页面可以包含不会更改页面中任何内容的视图模型...并且您可以有一个重定向到可编辑页面的编辑按钮。
通过身份验证保护该页面。因此,在编辑之前,您将被要求验证您的角色
。否则,您的 ViewModel 无法做出决定,它位于服务层。
You can create duplicate pages and one page can contain the view model which doesn't changes anything in the page... AND you can have an EDIT button which redirects to the editable page.
Make that page protected with authentication. SO you will asked to authenticate as your role before you can edit it
OTHERWISE.. there is no way your ViewModel can make decisions, its on the Service Layer.