title: pass token to nswag apiclient in .Netcore
body: <p>I have generated my <code>APIclient</code> Code using <code>Nswagstudio</code> as you can see :</p>
  <pre><code>//----------------------
  // &lt;auto-generated&gt;
  //     Generated using the NSwag toolchain v13.6.1.0 (NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
  // &lt;/auto-generated&gt;
  //----------------------
  
  #pragma warning disable 108 // Disable &quot;CS0108 '{derivedDto}.ToJson()' hides inherited member '{dtoBase}.ToJson()'. Use the new keyword if hiding was intended.&quot;
  #pragma warning disable 114 // Disable &quot;CS0114 '{derivedDto}.RaisePropertyChanged(String)' hides inherited member 'dtoBase.RaisePropertyChanged(String)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.&quot;
  #pragma warning disable 472 // Disable &quot;CS0472 The result of the expression is always 'false' since a value of type 'Int32' is never equal to 'null' of type 'Int32?'
  #pragma warning disable 1573 // Disable &quot;CS1573 Parameter '...' has no matching param tag in the XML comment for ...
  #pragma warning disable 1591 // Disable &quot;CS1591 Missing XML comment for publicly visible type or member ...&quot;
  
  namespace MyNamespace
  {
      using System = global::System;
      
      [System.CodeDom.Compiler.GeneratedCode(&quot;NSwag&quot;, &quot;13.6.1.0 (NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0))&quot;)]
      public partial interface IDefault1Client
      {
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          System.Threading.Tasks.Task&lt;Jwttoken&gt; CreateTokenAsync();
      
          /// &lt;param name=&quot;cancellationToken&quot;&gt;A cancellation token that can be used by other objects or threads to receive notice of cancellation.&lt;/param&gt;
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          System.Threading.Tasks.Task&lt;Jwttoken&gt; CreateTokenAsync(System.Threading.CancellationToken cancellationToken);
      
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          System.Threading.Tasks.Task&lt;System.Collections.Generic.ICollection&lt;string&gt;&gt; ReturnListAsync();
      
          /// &lt;param name=&quot;cancellationToken&quot;&gt;A cancellation token that can be used by other objects or threads to receive notice of cancellation.&lt;/param&gt;
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          System.Threading.Tasks.Task&lt;System.Collections.Generic.ICollection&lt;string&gt;&gt; ReturnListAsync(System.Threading.CancellationToken cancellationToken);
      
      }
      
      [System.CodeDom.Compiler.GeneratedCode(&quot;NSwag&quot;, &quot;13.6.1.0 (NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0))&quot;)]
      public partial class Default1Client : IDefault1Client
      {
          private string _baseUrl = &quot;&quot;;
          private System.Lazy&lt;Newtonsoft.Json.JsonSerializerSettings&gt; _settings;
      
          public Default1Client(string baseUrl)
          {
              BaseUrl = baseUrl; 
              _settings = new System.Lazy&lt;Newtonsoft.Json.JsonSerializerSettings&gt;(CreateSerializerSettings);
          }
      
          private Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
          {
              var settings = new Newtonsoft.Json.JsonSerializerSettings();
              UpdateJsonSerializerSettings(settings);
              return settings;
          }
      
          public string BaseUrl 
          {
              get { return _baseUrl; }
              set { _baseUrl = value; }
          }
      
          protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }
      
          partial void UpdateJsonSerializerSettings(Newtonsoft.Json.JsonSerializerSettings settings);
          partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, string url);
          partial void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder);
          partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response);
      
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          public System.Threading.Tasks.Task&lt;Jwttoken&gt; CreateTokenAsync()
          {
              return CreateTokenAsync(System.Threading.CancellationToken.None);
          }
      
          /// &lt;param name=&quot;cancellationToken&quot;&gt;A cancellation token that can be used by other objects or threads to receive notice of cancellation.&lt;/param&gt;
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          public async System.Threading.Tasks.Task&lt;Jwttoken&gt; CreateTokenAsync(System.Threading.CancellationToken cancellationToken)
          {
              var urlBuilder_ = new System.Text.StringBuilder();
              urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : &quot;&quot;).Append(&quot;/api/Default1/CreateToken&quot;);
      
              var client_ = await CreateHttpClientAsync(cancellationToken).ConfigureAwait(false);
              try
              {
                  using (var request_ = new System.Net.Http.HttpRequestMessage())
                  {
                      request_.Method = new System.Net.Http.HttpMethod(&quot;GET&quot;);
                      request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse(&quot;text/plain&quot;));
      
                      PrepareRequest(client_, request_, urlBuilder_);
                      var url_ = urlBuilder_.ToString();
                      request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                      PrepareRequest(client_, request_, url_);
      
                      var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
                      try
                      {
                          var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ =&gt; h_.Key, h_ =&gt; h_.Value);
                          if (response_.Content != null &amp;&amp; response_.Content.Headers != null)
                          {
                              foreach (var item_ in response_.Content.Headers)
                                  headers_[item_.Key] = item_.Value;
                          }
      
                          ProcessResponse(client_, response_);
      
                          var status_ = ((int)response_.StatusCode).ToString();
                          if (status_ == &quot;200&quot;) 
                          {
                              var objectResponse_ = await ReadObjectResponseAsync&lt;Jwttoken&gt;(response_, headers_).ConfigureAwait(false);
                              return objectResponse_.Object;
                          }
                          else
                          if (status_ != &quot;200&quot; &amp;&amp; status_ != &quot;204&quot;)
                          {
                              var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); 
                              throw new ApiException(&quot;The HTTP status code of the response was not expected (&quot; + (int)response_.StatusCode + &quot;).&quot;, (int)response_.StatusCode, responseData_, headers_, null);
                          }
              
                          return default(Jwttoken);
                      }
                      finally
                      {
                          if (response_ != null)
                              response_.Dispose();
                      }
                  }
              }
              finally
              {
                  if (client_ != null)
                      client_.Dispose();
              }
          }
      
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          public System.Threading.Tasks.Task&lt;System.Collections.Generic.ICollection&lt;string&gt;&gt; ReturnListAsync()
          {
              return ReturnListAsync(System.Threading.CancellationToken.None);
          }
      
          /// &lt;param name=&quot;cancellationToken&quot;&gt;A cancellation token that can be used by other objects or threads to receive notice of cancellation.&lt;/param&gt;
          /// &lt;returns&gt;Success&lt;/returns&gt;
          /// &lt;exception cref=&quot;ApiException&quot;&gt;A server side error occurred.&lt;/exception&gt;
          public async System.Threading.Tasks.Task&lt;System.Collections.Generic.ICollection&lt;string&gt;&gt; ReturnListAsync(System.Threading.CancellationToken cancellationToken)
          {
              var urlBuilder_ = new System.Text.StringBuilder();
              urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : &quot;&quot;).Append(&quot;/api/Default1/ReturnList&quot;);
      
              var client_ = await CreateHttpClientAsync(cancellationToken).ConfigureAwait(false);
              try
              {
                  using (var request_ = new System.Net.Http.HttpRequestMessage())
                  {
                      request_.Method = new System.Net.Http.HttpMethod(&quot;GET&quot;);
                      request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse(&quot;text/plain&quot;));
      
                      PrepareRequest(client_, request_, urlBuilder_);
                      var url_ = urlBuilder_.ToString();
                      request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                      PrepareRequest(client_, request_, url_);
      
                      var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
                      try
                      {
                          var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ =&gt; h_.Key, h_ =&gt; h_.Value);
                          if (response_.Content != null &amp;&amp; response_.Content.Headers != null)
                          {
                              foreach (var item_ in response_.Content.Headers)
                                  headers_[item_.Key] = item_.Value;
                          }
      
                          ProcessResponse(client_, response_);
      
                          var status_ = ((int)response_.StatusCode).ToString();
                          if (status_ == &quot;200&quot;) 
                          {
                              var objectResponse_ = await ReadObjectResponseAsync&lt;System.Collections.Generic.ICollection&lt;string&gt;&gt;(response_, headers_).ConfigureAwait(false);
                              return objectResponse_.Object;
                          }
                          else
                          if (status_ != &quot;200&quot; &amp;&amp; status_ != &quot;204&quot;)
                          {
                              var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); 
                              throw new ApiException(&quot;The HTTP status code of the response was not expected (&quot; + (int)response_.StatusCode + &quot;).&quot;, (int)response_.StatusCode, responseData_, headers_, null);
                          }
              
                          return default(System.Collections.Generic.ICollection&lt;string&gt;);
                      }
                      finally
                      {
                          if (response_ != null)
                              response_.Dispose();
                      }
                  }
              }
              finally
              {
                  if (client_ != null)
                      client_.Dispose();
              }
          }
      
          protected struct ObjectResponseResult&lt;T&gt;
          {
              public ObjectResponseResult(T responseObject, string responseText)
              {
                  this.Object = responseObject;
                  this.Text = responseText;
              }
      
              public T Object { get; }
      
              public string Text { get; }
          }
      
          public bool ReadResponseAsString { get; set; }
          
          protected virtual async System.Threading.Tasks.Task&lt;ObjectResponseResult&lt;T&gt;&gt; ReadObjectResponseAsync&lt;T&gt;(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary&lt;string, System.Collections.Generic.IEnumerable&lt;string&gt;&gt; headers)
          {
              if (response == null || response.Content == null)
              {
                  return new ObjectResponseResult&lt;T&gt;(default(T), string.Empty);
              }
          
              if (ReadResponseAsString)
              {
                  var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                  try
                  {
                      var typedBody = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;T&gt;(responseText, JsonSerializerSettings);
                      return new ObjectResponseResult&lt;T&gt;(typedBody, responseText);
                  }
                  catch (Newtonsoft.Json.JsonException exception)
                  {
                      var message = &quot;Could not deserialize the response body string as &quot; + typeof(T).FullName + &quot;.&quot;;
                      throw new ApiException(message, (int)response.StatusCode, responseText, headers, exception);
                  }
              }
              else
              {
                  try
                  {
                      using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                      using (var streamReader = new System.IO.StreamReader(responseStream))
                      using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(streamReader))
                      {
                          var serializer = Newtonsoft.Json.JsonSerializer.Create(JsonSerializerSettings);
                          var typedBody = serializer.Deserialize&lt;T&gt;(jsonTextReader);
                          return new ObjectResponseResult&lt;T&gt;(typedBody, string.Empty);
                      }
                  }
                  catch (Newtonsoft.Json.JsonException exception)
                  {
                      var message = &quot;Could not deserialize the response body stream as &quot; + typeof(T).FullName + &quot;.&quot;;
                      throw new ApiException(message, (int)response.StatusCode, string.Empty, headers, exception);
                  }
              }
          }
      
          private string ConvertToString(object value, System.Globalization.CultureInfo cultureInfo)
          {
              if (value is System.Enum)
              {
                  string name = System.Enum.GetName(value.GetType(), value);
                  if (name != null)
                  {
                      var field = System.Reflection.IntrospectionExtensions.GetTypeInfo(value.GetType()).GetDeclaredField(name);
                      if (field != null)
                      {
                          var attribute = System.Reflection.CustomAttributeExtensions.GetCustomAttribute(field, typeof(System.Runtime.Serialization.EnumMemberAttribute)) 
                              as System.Runtime.Serialization.EnumMemberAttribute;
                          if (attribute != null)
                          {
                              return attribute.Value != null ? attribute.Value : name;
                          }
                      }
          
                      return System.Convert.ToString(System.Convert.ChangeType(value, System.Enum.GetUnderlyingType(value.GetType()), cultureInfo));
                  }
              }
              else if (value is bool) 
              {
                  return System.Convert.ToString(value, cultureInfo).ToLowerInvariant();
              }
              else if (value is byte[])
              {
                  return System.Convert.ToBase64String((byte[]) value);
              }
              else if (value != null &amp;&amp; value.GetType().IsArray)
              {
                  var array = System.Linq.Enumerable.OfType&lt;object&gt;((System.Array) value);
                  return string.Join(&quot;,&quot;, System.Linq.Enumerable.Select(array, o =&gt; ConvertToString(o, cultureInfo)));
              }
          
              return System.Convert.ToString(value, cultureInfo);
          }
      }
  
      [System.CodeDom.Compiler.GeneratedCode(&quot;NJsonSchema&quot;, &quot;10.1.21.0 (Newtonsoft.Json v12.0.0.0)&quot;)]
      public partial class Jwttoken 
      {
          [Newtonsoft.Json.JsonProperty(&quot;token&quot;, Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
          public string Token { get; set; }
      
      
      }
  
      [System.CodeDom.Compiler.GeneratedCode(&quot;NSwag&quot;, &quot;13.6.1.0 (NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0))&quot;)]
      public partial class ApiException : System.Exception
      {
          public int StatusCode { get; private set; }
  
          public string Response { get; private set; }
  
          public System.Collections.Generic.IReadOnlyDictionary&lt;string, System.Collections.Generic.IEnumerable&lt;string&gt;&gt; Headers { get; private set; }
  
          public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary&lt;string, System.Collections.Generic.IEnumerable&lt;string&gt;&gt; headers, System.Exception innerException) 
              : base(message + &quot;\n\nStatus: &quot; + statusCode + &quot;\nResponse: \n&quot; + response.Substring(0, response.Length &gt;= 512 ? 512 : response.Length), innerException)
          {
              StatusCode = statusCode;
              Response = response; 
              Headers = headers;
          }
  
          public override string ToString()
          {
              return string.Format(&quot;HTTP Response: \n\n{0}\n\n{1}&quot;, Response, base.ToString());
          }
      }
  
      [System.CodeDom.Compiler.GeneratedCode(&quot;NSwag&quot;, &quot;13.6.1.0 (NJsonSchema v10.1.21.0 (Newtonsoft.Json v12.0.0.0))&quot;)]
      public partial class ApiException&lt;TResult&gt; : ApiException
      {
          public TResult Result { get; private set; }
  
          public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary&lt;string, System.Collections.Generic.IEnumerable&lt;string&gt;&gt; headers, TResult result, System.Exception innerException) 
              : base(message, statusCode, response, headers, innerException)
          {
              Result = result;
          }
      }
  
  }
  </code></pre>
  <p>So I have added this code to my MVC core endpoint with these configurations</p>
  <pre><code>  services.AddScoped&lt;IDefault1Client&gt;(provider =&gt;
              {
                  return new Default1Client(&quot;https://localhost:44381&quot;);
              });
  </code></pre>
  <p>My apli using JWT as a security pattern.But myproblem is how can I pass my token into httpclient of the above code thatis generated by nswag studio .?</p>