.net – 如何在请求标头上调用需要承载令牌的NSwag客户端方法?

我没有准确了解NSwag如何与IdentityServerX承载令牌交互并按常规添加请求标头?我的主机api应用程序使用LDAP身份验证实现IdentityServer3,据我所知;如果任何主机需要令牌进行身份验证,那么任何客户端都必须在请求头上发送它.那么我如何在为NSwag客户工作时处理它呢?

任何想法都赞赏.
谢谢.

我已经通过部分方法解决了这个问题.
我的例子是:

CampaignClient.cs

public partial class CampaignClient
{
    partial void PrepareRequest(HttpClient request, ref string url);

    partial void ProcessResponse(HttpClient request, HttpResponseMessage response);

    //some codes...
}

CampaignClient.Extensions.cs – 部分类:

public partial class CampaignClient
{
    private readonly IRequestContext _requestContext;
    private readonly IStartupConfiguration _startupConfiguration;

    public CampaignClient(IRequestContext requestContext, IStartupConfiguration startupConfiguration)
    {
        _requestContext = requestContext;
        _startupConfiguration = startupConfiguration;
    }

    partial void PrepareRequest(HttpClient request, ref string url)
    {
        request.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _requestContext.GetBearerTokenOrTriggerUnauthException());
    }
}

方法覆盖已经救了我!

翻译自:https://stackoverflow.com/questions/40025744/how-to-invoke-a-nswag-client-method-that-needs-bearer-token-on-request-header

转载注明原文:.net – 如何在请求标头上调用需要承载令牌的NSwag客户端方法?