Warm tip: This article is reproduced from serverfault.com, please click

c#-如何使用 api 连接器和 asp net core web api 通过自定义声明丰富 azure b2c 令牌

(c# - How to enrich azure b2c token with custom claims using api connectors and asp net core web api)

发布于 2021-10-13 21:59:54

我有一个用户流 B2C_1_singupsingin1 我添加了一个 api 连接器,将它嵌入到这个流和 API 调用的端点 url 中。使用的文章: https ://docs.microsoft.com/en-us/azure/active-directory-b2c/add-api-connector-token-enrichment?pivots=b2c-user-flow

从文章中可以清楚地看出,API 连接器具体化为 HTTP POST 请求,发送自定义属性。

我的 web api 有一个带有代码的端点:

[HttpPost("enrich")]
public IActionResult Enrich([FromBody] JsonElement body)
{
    var responseProperties = new Dictionary<string, object> //for example
    {
        { "version", "1.0.0" },
        { "action", "Continue" },
        { "postalCode", "12349" },
        { "userId", 123 } 
    };

    return new JsonResult(responseProperties) { StatusCode = 200 };
}

当我开始一个自定义流程时,一切正常,我在 api 中到达那个端点。但是有一个问题 JsonElement body 不包含自定义属性。里面我看到了body.ValueKind = Undefined告诉我我做错了什么?

而且,毕竟,我想添加一个自定义的“userId”声明,其中包含我的数据库中的一些值。使其包含在后续发行的令牌中。上面的代码对此是否正确?

Questioner
Александр Климук
Viewed
0
Steffen 2021-11-13 04:58:49

你的代码很好。只需在 postalCode 和 userId 前添加“ extension_ ”即可。

    [HttpPost("log")]
    public IActionResult Log([FromBody] JsonElement body)
    {

        var responseProperties = new Dictionary<string, object> 
        {
            { "version", "1.0.0" },
            { "action", "Continue" },
            { "extension_Role", "admin" },
        };

        return new JsonResult(responseProperties) { StatusCode = 200 };
    }

在我的 Azure AD B2C 中,我有一个名为“角色”的自定义属性。

但是在调试模式下,我看到所有自定义属性extension_都设置为前缀...

因此,通过将其添加到 responseProperties 似乎可以正常工作。

在此处输入图像描述

在此处输入图像描述