简体   繁体   English

.net核心2 mvc中相同操作的不同路由和身份验证

[英]different route and auth for the same action in the .net core 2 mvc

I have an action in my ASP.NET Core controller. 我在ASP.NET Core控制器中有一个操作。 This action has multiple routes: 此操作有多个路线:

public class TestController
{
    [HttpGet("test1")]
    [HttpGet("test2")]
    public async Task<object> Test()
    {
        //do something
    }
}

These two routes require different access rights, the following: 这两条路由需要不同的访问权限,如下所示:

  • route: test/test1 need to have role1 permission 路线: test/test1需要具有role1权限
  • route: test/test2 need to have role2 permission 路线: test/test2需要具有role2权限

I'm override the HttpGet class and implement IAuthorizationFilter interface: 我重写HttpGet类并实现IAuthorizationFilter接口:

public class AuthGet : HttpGet, IAuthorizationFilter
{
    public AuthGet(string template,string roleName) : base(template)
    {
    }
    public void OnAuthorization(AuthorizationFilterContext context)
    {
        //check auth
    }
}

Then, I'm replacing HttpGet with AuthGet . 然后,我将HttpGet替换为AuthGet This way, both AuthGet's OnAuthorization will execute. 这样,两个AuthGet的OnAuthorization都将执行。 But I can't distinguish between test/test1 and test/test2 . 但是我无法区分test/test1test/test2

How do I determine different permissions based on different routes on an Action ? 如何根据Action上的不同路由确定不同的权限?

There is no reason to keep this to a single method. 没有理由将其保留为单一方法。 Simply create private method to do the actual work and create two Actions that simply the return the value of that private method: 只需创建private方法来完成实际工作,然后创建两个Action即可简单地返回该private方法的值:

[AuthGet("test1", "role1")]
public async Task<object> Test1()
{
    return await Test(1);
}

[AuthGet("test2", "role2")]
public async Task<IActionResult> Test2()
{
    return await Test(2);
}

private async Task<object> Test(int testRoute)
{
    if (testRoute == 1)
    {
        //do something
    }
    else
    {
        // do somthing else
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .net 核心 5 MVC 中重载操作方法但路由模板不同(名称为 controller,名称为 controller) - Overload action method but different route template(with controller name and without controller name) in .net core 5 MVC asp.net core mvc中的自定义路由和动作名称 - Custom route and action names in asp.net core mvc ASP.Net Core MVC-身份验证上下文是否与部分视图不同? - ASP.Net Core MVC - Auth Context different for partial views? ASP.Net Core 6 MVC 中一项操作的两种不同路径 - Two Different Routes For One Action in ASP.Net Core 6 MVC .Net MVC控制器使用相同的cshtml作为视图使用不同的路由名称 - .Net MVC Controller different route name using the same cshtml as view 如何在.NET-Core Mvc中处理两条路由并定向到一个动作 - How to handle two route and direct to one Action in .NET-Core Mvc asp-all-route-data 不调用 Asp.Net Core MVC 中的操作方法 - asp-all-route-data not calling action method in Asp.Net Core MVC 在Asp.Net Core MVC控制器操作中未获取路由ID - Route id not being picked up in Asp.Net Core MVC controller action ASP.NET 核心 MVC - Razor - asp-action 路由问题 - ASP.NET Core MVC - Razor - asp-action route issue 创建到特定操作的不同路由mvc 6 - Creating a different route to a specific action mvc 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM