身份服务器 4 api 项目中的操作过载

action overload in identityserver 4 api project

我会添加 2 api 到 net core 3.1 api 项目的控制器中:

第一个接受 access_token 作为参数,另一个称为 whitout 参数,从 headers 中检索令牌。

这个控制器:

[Route("entitlements")]
public class EntitlementsController : ControllerBase
{

    [Route("entitlements/get/{access_token}")]
    public async Task<IActionResult> Get([FromQuery] string access_token)
    {
        EntitlementsResult oResult = GetEntitlements(access_token);
        return new JsonResult(oResult);
    }

    [Route("entitlements")]
    public async Task<IActionResult> Get()
    {
        var access_token = await GetAccessTokenFromHeaders();
        EntitlementsResult oResult = GetEntitlements(token);
        return new JsonResult(oResult);
    }
}

但我收到 404 not found 从客户端调用此代码的操作:

public async Task<IActionResult> GetEntitlements()
{
        var accessToken = await HttpContext.GetTokenAsync("access_token");

        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        string apiUrl = Startup.StaticConfig.GetValue<string>("IdentityServer:api");
        var content = await client.GetStringAsync($"{apiUrl}/entitlements");
        ViewBag.Json = content;
        return View("json");
    }

因为动作和控制器都包含[Route("entitlements")]属性,所以正确的url应该是:{apiUrl}/[ControllerRoute]/[ActionRoute],修改如下:

var content = await client.GetStringAsync($"{apiUrl}/entitlements/entitlements");