覆盖AspNet.Security.OpenIdConnect.Server中的TokenEndPoint

时间:2015-12-03 19:43:42

标签: c# asp.net-core openid openid-connect aspnet-contrib

与此帖相关的问题:Configure the authorization server endpoint

使用上面的例子,我可以获得令牌。以前可以通过骑车获得更多信息

public override Task TokenEndpoint(OAuthTokenEndpointContext context)
        {
            foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
            {
                context.AdditionalResponseParameters.Add(property.Key, property.Value);
            }

            return Task.FromResult<object>(null);
        }

如何在当前的

实现中实现这一目标
public override Task TokenEndpoint(TokenEndpointContext context){
}

谢谢!

1 个答案:

答案 0 :(得分:4)

您最好的选择是直接使用ApplyTokenResponse事件来更新返回给客户端应用程序的JSON有效内容。与AdditionalResponseParameters不同,它允许您添加 - 或删除 - 几乎任何东西:对象,数组,字符串,整数......

以下是如何做到的:

public override Task ApplyTokenResponse(ApplyTokenResponseContext context)
{
    // Only add the custom parameters if the response is not a token error response.
    if (string.IsNullOrEmpty(context.Error))
    {
        context.Response["custom-property-1"] = "custom-value";

        context.Response["custom-property-2"] = JArray.FromObject(new[]
        {
            "custom-value-1",
            "custom-value-2"
        });
    }

    return Task.FromResult(0);
}