如何使IdentityServer将用户身份添加到访问令牌? - c#

简短:我的客户端从IdentityServer示例服务器检索访问令牌,然后将其传递给我的WebApi。在我的控制器中,this.HttpContext.User.GetUserId()返回null(尽管用户还有其他要求)。我怀疑访问令牌中没有名称标识声明。如何使IdentityServer包含它?

到目前为止,我已经尝试过:

从混合流切换到隐式流(随机尝试)
我在IdSvrHost范围定义中添加了

声明= {new ScopeClaim(ClaimTypes.NameIdentifier,alwaysInclude:true)}
我在IdSvrHost客户端定义中添加了

索赔= {new Claim(ClaimTypes.NameIdentifier,“ 42”)}

(也是随机尝试)

我还在范围定义中尝试了其他范围,但都没有出现。看来,nameidentity通常包含在身份令牌中,但是对于我所知道的大多数公共API,您都不会向服务器提供身份令牌。

更多细节:
IdSrvHost和Api位于不同的主机上。
控制器具有[授权]。实际上,我可以看到其他要求。
Api配置有

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

app.UseIdentityServerAuthentication(options => {
    options.Authority = "http://localhost:22530/";

    // TODO: how to use multiple optional scopes?
    options.ScopeName = "borrow.slave";
    options.AdditionalScopes = new[] { "borrow.receiver", "borrow.manager" };

    options.AutomaticAuthenticate = true;
    options.AutomaticChallenge = true;
});

范围:

public static Scope Slave { get; } = new Scope {
    Name = "borrow.slave",
    DisplayName = "List assigned tasks",
    Type = ScopeType.Resource,

    Claims = {
        new ScopeClaim(ClaimTypes.NameIdentifier, alwaysInclude: true),
    },
};

和客户:

new Client {
    ClientId = "borrow_node",
    ClientName = "Borrow Node",

    Flow = Flows.Implicit,

    RedirectUris = new List<string>
    {
        "borrow_node:redirect-target",
    },

    Claims = { new Claim(ClaimTypes.NameIdentifier, "42") },

    AllowedScopes = {
        StandardScopes.OpenId.Name,
        //StandardScopes.OfflineAccess.Name,
        BorrowScopes.Slave.Name,
    },
}

验证URI:

request.CreateAuthorizeUrl(
            clientId: "borrow_node",
            responseType: "token",
            scope: "borrow.slave",
            redirectUri: "borrow_node:redirect-target",
            state: state,
            nonce: nonce);

我也尝试过

request.CreateAuthorizeUrl(
            clientId: "borrow_node",
            responseType: "id_token token",
            scope: "openid borrow.slave",
            redirectUri: "borrow_node:redirect-target",
            state: state,
            nonce: nonce);

参考方案

Hooray,当我偶然发现此页面时,我找到了答案:https://github.com/IdentityServer/IdentityServer3.Samples/issues/173

显然,用户身份在访问令牌的“子”声明中传递。因为我盲目复制了API示例,所以它的配置包括

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

这实际上阻止了我的API将“子”声明映射到nameidentifier。删除此行后,经过身份验证的控制器的HttpContext.User.GetUserId()可正确返回用户ID。

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

Json到php,json_decode返回NULL - php

我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…