用于过滤 asp.net 个核心 REST 方法的中间件
Middleware for filtering asp.net core REST methods
我有一个带有多个 REST 操作的 .net 核心应用程序(请参阅下面的代码),类似于以下内容:
namespace Controllers
{
[Route("system")]
public class SystemController : Controller
{
// This is a public access method
[HttpGet("dictionaries/{name}")]
public List GetDictionary(HttpRequestMessage requestMsg, string name)
{
// etc
}
// This function shall be accessible only by an admin role
<b>[AdminRole]</b>
[HttpPost("dictionaries/{name}")]
public IActionResult PostDictionary(HttpRequestMessage requestMsg, string name)
{
// etc
}
}
}
我想将某些操作标记为只能由特定角色(即管理员)访问。一种优雅的方法是使用属性。
现在我想确定 捕获要根据 URL 调用的 C# 方法的正确 Middleware
实现,然后获取使用反射的角色属性(如果有的话),这样我就可以阻止未经授权的调用。
请指教
我想提请注意,以下方法仅适用于您出于某种原因不想使用内置 Role based Authorization(如问题评论中所标记)的情况。
如果您创建全局 Action Filter(它是 MVC 的一部分,因此可以使用 "controller logic" 进行操作),您可以从 ActionExecutingContext
:[=17 获取所有需要的信息=]
public class SampleActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
//Provides information about an action method, such as its name, controller, parameters, attributes, and filters.
var actionDescriptor = context.ActionDescriptor;
//Gets the controller instance containing the action.
var controller = context.Controller;
// Gets the arguments to pass when invoking the action. Keys are parameter names.
var actionArgs = context.ActionArguments;
}
...
}
context.ActionDescriptor
可以转换为 ControllerActionDescriptor。这允许直接使用以下属性:
public class ControllerActionDescriptor : ActionDescriptor
{
public string ControllerName { get; set; }
public virtual string ActionName { get; set; }
public MethodInfo MethodInfo { get; set; }
public TypeInfo ControllerTypeInfo { get; set; }
...
}
不确定是否可以为此使用中间件,因为控制器是 MVC 中间件的一部分。如果您将中间件放在它之前,则该管道步骤还没有 "controller" 逻辑。如果之后 - 你想要什么就太晚了。
我有一个带有多个 REST 操作的 .net 核心应用程序(请参阅下面的代码),类似于以下内容:
namespace Controllers
{
[Route("system")]
public class SystemController : Controller
{
// This is a public access method
[HttpGet("dictionaries/{name}")]
public List GetDictionary(HttpRequestMessage requestMsg, string name)
{
// etc
}
// This function shall be accessible only by an admin role
<b>[AdminRole]</b>
[HttpPost("dictionaries/{name}")]
public IActionResult PostDictionary(HttpRequestMessage requestMsg, string name)
{
// etc
}
}
}
我想将某些操作标记为只能由特定角色(即管理员)访问。一种优雅的方法是使用属性。
现在我想确定 捕获要根据 URL 调用的 C# 方法的正确 Middleware
实现,然后获取使用反射的角色属性(如果有的话),这样我就可以阻止未经授权的调用。
请指教
我想提请注意,以下方法仅适用于您出于某种原因不想使用内置 Role based Authorization(如问题评论中所标记)的情况。
如果您创建全局 Action Filter(它是 MVC 的一部分,因此可以使用 "controller logic" 进行操作),您可以从 ActionExecutingContext
:[=17 获取所有需要的信息=]
public class SampleActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
//Provides information about an action method, such as its name, controller, parameters, attributes, and filters.
var actionDescriptor = context.ActionDescriptor;
//Gets the controller instance containing the action.
var controller = context.Controller;
// Gets the arguments to pass when invoking the action. Keys are parameter names.
var actionArgs = context.ActionArguments;
}
...
}
context.ActionDescriptor
可以转换为 ControllerActionDescriptor。这允许直接使用以下属性:
public class ControllerActionDescriptor : ActionDescriptor
{
public string ControllerName { get; set; }
public virtual string ActionName { get; set; }
public MethodInfo MethodInfo { get; set; }
public TypeInfo ControllerTypeInfo { get; set; }
...
}
不确定是否可以为此使用中间件,因为控制器是 MVC 中间件的一部分。如果您将中间件放在它之前,则该管道步骤还没有 "controller" 逻辑。如果之后 - 你想要什么就太晚了。