gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 安全性 : how to check if a controller method is allowed to execute under current user's perrmissions

转载 作者:行者123 更新时间:2023-12-01 02:56:36 25 4
gpt4 key购买 nike

给定一个 ASP.NET MVC Controller 类声明:

public class ItemController : Controller
{
public ActionResult Index()
{
// ...
}

public ActionResult Details()
{
// ...
}

[Authorize(Roles="Admin, Editor")]
public ActionResult Edit()
{
// ...
}

[Authorize(Roles="Admin")]
public ActionResult Delete()
{
// ..
}
}

我需要在这个类中反射(reflect)一个方法列表,这些方法可以用当前用户的权限调用。

请分享一些关于在这种情况下可以做什么的想法。

最佳答案

那么对于新问题,请按照以下思路思考:

new ReflectedControllerDescriptor(typeof(ItemController)).GetCanonicalActions()

可用于返回所有可用操作的列表。我在工作中没有可用的 ASP.NET MVC,所以我无法真正检查它返回的 ActionDescriptor 是否包含一些参数,说明哪些成员可以执行它们。

http://msdn.microsoft.com/en-us/library/system.web.mvc.actiondescriptor_members%28v=VS.90%29.aspx

那是 ActionDescriptor 的成员,你也许可以在那里找到一些东西。如果我能弄明白,我今晚会看看,这让我有点好奇。

没有适用于所有应用程序的通用用户登录/身份验证系统,因此这真的不可能创建“通用解决方案”。您可以创建自己的用户登录和授权类,然后将自己的注释添加到要执行的方法中,但它具有与 asp.net mvc 系统相同的限制,它仅适用于您的登录/授权系统(或任何人扩展该系统)。

关于asp.net-mvc - ASP.NET MVC 安全性 : how to check if a controller method is allowed to execute under current user's perrmissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2772578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com