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 的 Add 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 只是与我的代码有关的 poco。