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.
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.
Are you generating a client for TypeScript or C#?
@grovesNL , C# client.
@lciocci Where can i find the "UserHttpClientCreationMethod:true" option ?
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
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
The documentation for this can be found here: https://github.com/NSwag/NSwag/wiki/SwaggerToCSharpClientGenerator#implement-a-base-class
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!
what is IRequestContext and IStartupConfiguration? I cannot find these interfaces
what is IRequestContext and IStartupConfiguration? I cannot find these interfaces
It's from the AWSSDK.Core package...
Most helpful comment
Thanks guys.
I've resolved as you said by partial method.
My example is:
CampaignClient.cs
CampaignClient.Extensions.cs - partial class:
Method override has saved me!
See also Stackoverflow