Skip to content

Instantly share code, notes, and snippets.

@fatagun
Last active August 6, 2019 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatagun/1a2c6e88a328e3666742ec3a74aca59a to your computer and use it in GitHub Desktop.
Save fatagun/1a2c6e88a328e3666742ec3a74aca59a to your computer and use it in GitHub Desktop.
var controllers = from type in myAssembly.GetTypes()
where type.Name.Contains("Controller")
where !type.IsAbstract
select type;
// bunun yerine ITypeLocator kullanabilirsin.
// _locator.FindClassesOfType<Controller>();
foreach(var controller in controllers)
{
var authorized = controller.GetCustomAttributes(true).Any(e => e.GetType().Equals(typeof(AuthorizeAttribute)));
if(authorized)
{
var controllerAttrib = controller.GetCustomAttribute(typeof(AuthorizeAttribute)) as AuthorizeAttribute;
var controllerName = controller.Name;
Console.WriteLine($"{controllerName} - {controllerAttrib.Roles} - {controllerAttrib.Policy} - {controllerAttrib.AuthenticationSchemes}");
var controllerMethods = controller.GetMethods();
foreach (var controllerMethod in controllerMethods)
{
var methodName = controllerMethod.Name;
var attribute = controllerMethod.GetCustomAttribute(typeof(AuthorizeAttribute)) as AuthorizeAttribute;
if(attribute != null)
{
Console.WriteLine($"{methodName} - {attribute.Roles} - {attribute.Policy} - {attribute.AuthenticationSchemes}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment