Search code examples
c#restjwtasp.net-core-2.0autorest

How do I get JWT working in Autorest generated SDK? (ASP.NET Core 2.0)


I want to be able to login to my identity database with user name and password and retreive a JWT. Then I want to use the JWT to access data securely from my API.

I found out that the SDK code generated by VS2017 uses an old version of autorest, so I have switched to using Azure Autorest

The api and the SDK are both ASP.NET Core 2.0

To generate the SDK I use

AutoRest -mynamespace mytrack.Client -CodeGenerator CSharp -Modeler 
Swagger -Input swagger.json -PackageName mytrack.client -AddCredentials true

The versions show as

AutoRest code generation utility [version: 2.0.4262; node: v8.11.2]

I have written my test as

using System;
using System.Threading.Tasks;
using Microsoft.Rest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using swagger; // my name space from the autorest command, not to be confused with swagger itself.
using swagger.Models;

namespace CoreClientTest
{
    [TestClass]
    public class MyTests
    {
        [TestMethod]
        public void TestMethod1()
        {
            try
            {
                GetMyJob().Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        private static async Task GetMyJob()
        {
            var tokenRequest = new TokenRequest
            {
                Username = "myusername",
                Password = "mypassword"
            };

            var credentials = new TokenCredentials("bearer token");
            var uri = new Uri("https://localhost:44348", UriKind.Absolute);
            var tokenClient = new Track3API(uri, credentials);
            var tokenResponse = await tokenClient.ApiRequestTokenPostWithHttpMessagesAsync(tokenRequest);
            var tokenContent = await tokenResponse.Response.Content.ReadAsStringAsync();
            var tokenString = JObject.Parse(tokenContent).GetValue("token").ToString();
            var creds2 = new TokenCredentials(tokenString);
            var client2 = new Track3API(uri, creds2);
            var result = await client2.ApiJobsByIdGetWithHttpMessagesAsync(1);
            var response = result.Response;
            Console.WriteLine(response.ToString());
        }
    }
}

I can see that the result has OK and I can see the token in it. I cant see the return job

The method in the api has

[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api/jobs")]
public class JobController : Controller
{


/// <summary>
/// Returns Job Header for Id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}", Name = "Get")]
public IActionResult Get(int id)
{
    var header1 = new JobHeader
    {
        JobNumber = "1234",
        Id = id,
        CustomerPurchaseOrderNumber = "fred"
    };
    return Ok(header1);
}

}


Solution

  • Finally it is working. I found a tip at Andrei Dzimchuk's blog on setting up the token

    using System;
    using System.Threading.Tasks;
    using Microsoft.Rest;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using swagger;
    using swagger.Models;
    
    namespace CoreClientTest
    {
        [TestClass]
        public class MyTests
        {
            [TestMethod]
            public void TestMethod1()
            {
                try
                {
                     GetMyJob().Wait();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
    
            private static async Task<JobHeader> GetMyJob()
            {
                var tokenRequest = new TokenRequest
                {
                    Username = "myusername",
                    Password = "mypassword"
                };
                var credentials = new TokenCredentials("bearer token");
                var uri = new Uri("https://localhost:44348", UriKind.Absolute);
                var tokenClient = new Track3API(uri, credentials);
                var tokenResponse = await tokenClient.ApiRequestTokenPostWithHttpMessagesAsync(tokenRequest);
                var tokenContent = await tokenResponse.Response.Content.ReadAsStringAsync();
                var tokenString = JObject.Parse(tokenContent).GetValue("token").ToString();
                var creds2 = new TokenCredentials(tokenString);
                var client2 = new Track3API(uri, creds2);
                var result = await client2.ApiJobsByIdGetWithHttpMessagesAsync(1);
                string resultContent = await result.Response.Content.ReadAsStringAsync();
                var job = JsonConvert.DeserializeObject<JobHeader>(resultContent);
                Console.WriteLine(job.JobNumber);
                return job;
    
            }
        }
    }