[英]ASP.Net MVC: How to iterate in authorize or restricted controllers and actions name
我想获取控制器和受保护的动作名称,并用控制器和动作名称填充我的课程。
[Authorize]
public class Test1Controller : Controller
{
public ActionResult Index1()
{
return View();
}
public ActionResult Index2()
{
return View();
}
[AllowAnonymous]
public ActionResult Index3()
{
return View();
}
}
[AllowAnonymous]
public class Test2Controller : Controller
{
[Authorize]
public ActionResult Index1()
{
return View();
}
[Authorize]
public ActionResult Index2()
{
return View();
}
public ActionResult Index3()
{
return View();
}
}
public class Test3Controller : Controller
{
public ActionResult Index1()
{
return View();
}
public ActionResult Index2()
{
return View();
}
public ActionResult Index3()
{
return View();
}
}
1) Test1Controller受保护,但Index3操作具有匿名访问权限。 所以我想以一种方式进行迭代,结果将得到Test1Controller名称和两个其动作名称,其中没有允许匿名属性。
2) Test2Controller具有匿名属性,但是其中两个动作受到保护。 所以我想获得Test2Controller名称和它的两个动作名称。
3) Test3Controller对其任何操作均无任何保护,因此当我在控制器和操作集合中进行迭代时,此控制器名称及其任何操作名称都不会出现。
下面的代码给我所有的控制器和动作名称,这不是我的要求
Assembly asm = Assembly.GetExecutingAssembly();
var controlleractionlists = asm.GetTypes()
.Where(type => typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
.Select(x => new { Controller = x.DeclaringType.Name.Replace("Controller",string.Empty), Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", ""))) })
.OrderBy(x => x.Controller).ThenBy(x => x.Action).ToList();
因此,请告诉我我需要在上述代码中进行什么样的更改,结果我将获得那些具有至少一个受保护动作的控制器名称。
请帮助我纠正代码。 谢谢
//for actions with AuthorizeAttribute
var actions = asm.GetTypes()
.Where(type => typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Where(m => m.GetCustomAttributes(typeof(System.Web.Mvc.AuthorizeAttribute), true).Any())
.Select(x => new { Controller = x.DeclaringType.Name.Replace("Controller", string.Empty), Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", ""))) })
.OrderBy(x => x.Controller).ThenBy(x => x.Action);
//for controllers with AuthorizeAttribute
var controllers = asm.GetTypes()
.Where(type => typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
.Select(type => type.GetTypeInfo())
.Where(type => type.GetCustomAttributes(typeof(System.Web.Mvc.AuthorizeAttribute), true).Any())
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Where(m => !m.GetCustomAttributes(typeof(System.Web.Mvc.AllowAnonymousAttribute), false).Any())
.Select(x => new { Controller = x.DeclaringType.Name.Replace("Controller", string.Empty), Action = x.Name, ReturnType = x.ReturnType.Name, Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", ""))) })
.OrderBy(x => x.Controller).ThenBy(x => x.Action);
var controlleractionlists = actions.Concat(controllers).ToList();
如果为操作和控制器级别设置了AuthorizeAttribute
,则此操作将导致两次结果,但可以轻松修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.