简体   繁体   中英

ASP.Net MVC: How to iterate in authorize or restricted controllers and actions name

i want to get controllers and protected action name and populate my class with controller and action name.

see the below code

[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 is protected but Index3 action has Anonymous access. so i want to iterate in such a way as a result i will get Test1Controller name and two its action name which has no allow Anonymous attribute.

2) Test2Controller has Anonymous attribute but two of its action is protected. so i want to get Test2Controller name and two of its action name.

3) Test3Controller has no protection for any of its action so this controller name and any of its action name will not come when i will iterate in controller and action collection.

the below code give me all controller and action name which is not my requirement

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();

so please tell me what kind of change i need to incorporate in above code as a result those controllers name i will get which has at least one protected action.

please help me with rectified code. thanks

//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();

If AuthorizeAttribute is set for action and controller level, this action will be in result twice but it can be fixed easly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM