如何从响应中提取JWT授权?

时间:2018-05-18 23:57:07

标签: c# jwt

我的单元测试如下

 [TestMethod]
    public async Task TestMethod1()
    {

        SvcRestClient client =
            new SvcRestClient(new Uri("http://localhost:44395"),
                new AnonymousCredential());

        var tokenRequest = new TokenRequest
        {
            Username = "myusername",
            Password = "p@ssword1"
        };

        tokenRequest.Validate();

       var tokenResponse =  await client.ApiRequestTokenPostWithHttpMessagesAsync(tokenRequest);

       var content = await tokenResponse.Response.Content.ReadAsStringAsync();

       Console.WriteLine(content);  // displays {"token":"XXX"}  where XXX is the token string

        var authorisation =  content;

        var result = client.ApiJobByIdGet(4, authorisation);  // errors  here


    }

错误是

Test method ClientSideTests.UnitTest1.TestMethod1 threw exception: 
Microsoft.Rest.SerializationException: Unable to deserialize the response. ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0

来自VS2017的添加REST API客户端的生成代码是

    /// <summary>
    /// Returns Job Header for Id
    /// </summary>
    /// <param name='operations'>
    /// The operations group for this extension method.
    /// </param>
    /// <param name='id'>
    /// </param>
    /// <param name='authorization'>
    /// access token
    /// </param>
    public static JobHeader ApiJobByIdGet(this ISvcRestClient operations, int id, string authorization)
    {
        return Task.Factory.StartNew(s => ((ISvcRestClient)s).ApiJobByIdGetAsync(id, authorization), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
    }

JobHeader只是我的代码的一部分。

0 个答案:

没有答案