块基于用户角色调用类方法asp.net mvc 3
我正在创建我的第一个 ASP.NET MVC 3 应用程序,但在根据用户角色创建类实例时遇到问题。
我有一个名为 Account 的类,它包含有关用户帐户的信息和一些允许我操作(更新)用户帐户信息的方法。例如 ChangeNickName、AddToGroup、RemoveFromGroup、Ban 等。
正如您所看到的,这些方法的问题是我不想允许任何人使用 AddToGroup 或 Ban 方法,而只允许具有指定角色的用户使用(我正在使用 ASP.NET 默认角色实现系统)
所以我想知道有没有一种方法可以将属性 [Authorize(Roles = "Admin")] 添加到我的 AddTogroupMethod 就像我在控制器方法上使用它一样
我知道,通过正确实现控制器,我不需要这样的东西,但我想确保我不允许任何人类的实例(或意外使用指定的方法)如果用户不属于指定角色。
因此,例如,如果我不小心在用户控制器中添加对 AddToGroup 方法的访问权限,我仍然能够阻止他们利用错误,因为附加到此方法的属性
如果我无法使用属性解决此问题,有没有办法使 VS如果我从 xy 类调用 AddToGroup 方法,则阻止我调试应用程序
如果您有任何想法如何使其工作,我愿意接受建议
比提前
I'm creating my first ASP.NET MVC 3 app and I have a problem with creating class instance based on user role.
I have a Class called Account and it contains information about user accounts and few methods which allow me to manipulate(update) user account information. For example ChangeNickName, AddToGroup, RemoveFromGroup, Ban and so on.
As you can see problem with these methods is that I dont want to allow anyone to use AddToGroup or Ban method but only user with specified role (i'm using ASP.NET default role implementation system)
So I wonder is there a way I could add attribute [Authorize(Roles = "Admin")] to my AddTogroupMethod just like I use it on Controller methods
I know that with proper implementation of controllers I dont need something like this but I want to make sure that I dont allow anyone to make instance of class (or use specified methods by accident) if user is not part of specified role.
So for example if I by accident add access to AddToGroup method in user controller I would still be able to prevent them from exploiting bug because of attribute attached to this method
If I can't solve this problem with attributes is there a way to make VS to block me from debugging application if I make a call to AddToGroup method from xy class
If you have any ides how to make this working I'm open to suggestions
Than in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该使用
AuthorizeAttribute
,但是您可以使用Roles.UserIsInRole
在代码中检查当前用户的角色。MVC 中有一些关于此的信息(程序集似乎在 3.5/4.0 之间发生了变化):
asp.net mvc -> Roles.IsUserInRole(用户名,角色)
You should probably use the
AuthorizeAttribute
, however you can check the role of a current user in code usingRoles.UserIsInRole
.There's a little info on this in MVC here (the assembly seems to have changed between 3.5/4.0):
asp.net mvc -> Roles.IsUserInRole(username,role)