Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to invoke a client method that needs bearer token on request header? #365

Closed
osoykan opened this issue Oct 13, 2016 · 10 comments
Closed

Comments

@osoykan
Copy link

osoykan commented Oct 13, 2016

I didn't get exactly how NSwag interact with IdentityServerX bearer tokens and adds it request header conventionally ?My host api application implements IdentityServer3 with LDAP auth, so as far as i understand; if any host needs to a token for authentication then any client must send it on request header. So how can i deal with it while working NSwag clients ?

Any idea appreciated.
Thanks.

@lciocci
Copy link

lciocci commented Oct 13, 2016

This should help (if you're using C#): https://github.com/NSwag/NSwag/wiki/SwaggerToCSharpClientGenerator

Look at the /ClientBaseClass:[class name] and /UseHttpClientCreationMethod:true parameters. The /ClientBaseClass:[class name] causes the client proxy to inherit from a base class, and the /UserHttpClientCreationMethod:true allows you to override the creation of the default HttpClient. It's in there where you can then add your bearer token in the HttpClient's request header.

@grovesNL
Copy link
Contributor

Are you generating a client for TypeScript or C#?

@osoykan
Copy link
Author

osoykan commented Oct 19, 2016

@grovesNL , C# client.

@lciocci Where can i find the "UserHttpClientCreationMethod:true" option ?

@lciocci
Copy link

lciocci commented Oct 19, 2016

There are probably a few ways to do this, but we use NSwag command line (it's a NuGet package). Taken from the documentation:

nswag swagger2csclient /input:MyWebService.json 
                       /classname:MyServiceClient 
                       /UseHttpClientCreationMethod:true
                       /namespace:MyNamespace
                       /output:MyServiceClient.cs

@RicoSuter
Copy link
Owner

RicoSuter commented Oct 19, 2016

In order to use the feature you also have to define a ClientBaseClass. See https://github.com/NSwag/NSwag/wiki/SwaggerToCSharpClientGeneratorSettings

nswag swagger2csclient /Input:MyWebService.json 
                       /Classname:MyServiceClient 
                       /ClientBaseClass:MyBaseClass
                       /UseHttpClientCreationMethod:true
                       /Namespace:MyNamespace
                       /Output:MyServiceClient.cs

@RicoSuter
Copy link
Owner

The documentation for this can be found here: https://github.com/NSwag/NSwag/wiki/SwaggerToCSharpClientGenerator#implement-a-base-class

@osoykan
Copy link
Author

osoykan commented Oct 21, 2016

Thanks guys.

I've resolved as you said by partial method.
My example is:

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 - partial class:

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());
    }
}

Method override has saved me!

See also Stackoverflow

@pratishthap
Copy link

what is IRequestContext and IStartupConfiguration? I cannot find these interfaces

@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented Apr 6, 2020

what is IRequestContext and IStartupConfiguration? I cannot find these interfaces

It's from the AWSSDK.Core package...

@rebeccapowell
Copy link

Is GetBearerTokenOrTriggerUnauthException a custom extension method that you wrote?

Thanks guys.

I've resolved as you said by partial method. My example is:

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 - partial class:

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());
    }
}

Method override has saved me!

See also Stackoverflow

Is GetBearerTokenOrTriggerUnauthException a custom extension method that you wrote for HttpRequestMessage? You're copying the bearer token from the incoming request and passing it onwards correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants