如何在C#MVC中检查方法是否具有AllowAnonymous属性

时间:2020-05-14 11:01:18

标签: c# asp.net-mvc

我有一个Controller,其中某些方法具有授权属性,而某些方法具有AllowAnonymous 所以,我想在调用时检查方法是Authorize或AllowAnonymous

这是我的方法名称为

的控制器
[AllowAnonymouse]

Public ActionResult Login(){}

所以在不同的类中,我想检查此方法是Authorize还是AllowAnonymous

1 个答案:

答案 0 :(得分:1)

string actionName = ViewContext.RouteData.Values["Action"]
MethodInfo method = type.GetMethod(actionName);
var attribute = method.GetCustomAttributes(typeof(DisplayNameAttribute), false);
if (attribute.Length > 0)
   actionName = ((DisplayNameAttribute)attribute[0]).DisplayName;
else 
   actionName = type.Name;

检查此链接:

Get attribute of current controller action in MVC View