在读取webapi 2令牌时,读取HttpResponseMessage.Content会抛出Newtonsoft.Json.JsonReaderException

时间:2014-03-02 16:47:20

标签: c# json security asp.net-web-api2

您好我已经编写了一段代码,应该从c#桌面应用程序登录到WebAPI 2站点,所有内容都可以正常工作但是我得到了一个带有如下消息的Newtonsoft.Json.JsonReaderException

  

读取字符串时出错。意外的令牌:StartObject。路径'',第1行,第1位。

我的代码如下。

static internal async Task<string> GetBearerToken(string siteUrl, string Username, string Password)
{
  HttpClient client = new HttpClient();
  client.BaseAddress = new Uri(siteUrl);
  client.DefaultRequestHeaders.Accept.Clear();

  HttpContent content = new StringContent("grant_type=password&username=" + Username + "&password=" + Password, Encoding.UTF8, "application/x-www-form-urlencoded");

  Task<HttpResponseMessage> responseTask = client.PostAsync("Token", content);
  HttpResponseMessage response = await responseTask;

  if (response.IsSuccessStatusCode)
  {
     Task<string> message = response.Content.ReadAsAsync<string>();

     return await message;
  }
  else
  {
     return null;
  }
}

fiddler报道的原始响应消息

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 593
Content-Type: application/json;charset=UTF-8
Expires: -1
Server: Microsoft-IIS/8.0
Set-Cookie: .AspNet.Cookies=vo5b0v_43BLYlfz-rYTZ-TSGi9Rg5jSd9bvKn9693e-Kx3mMI1JVX1Sk-696f_fnPEFPRwFrNWvdMfDWUTWBElfQF3UfcUAxEE5aU5zRgI40sYKapXXnC2ucIiNKCqVsceve0cxNQYVAIr_YhMNjFLRqBX7H3BTPVKGist2AeUkWw6S4VNijx5iQhvWrAvF4xlJSznCiykNqR-QHD_ZLM5-H3GZoghrkvMpr27eXY4mLIqg4lwV2Qah0gQlXnjuWbHHZqLj5HcID1S7_OfPldBE3YqBOR2JxHLITg3yPw3lbXNkHc1UDdG9HExq0faJptz0SBqd8tIeZ7buoJTZ4LHV0TcYSEs4HZ3-Bd84XX7XeWPa5qnTaAJqXaW2FAigD38a9ASr15r5wnzWv9xQxlg; path=/; HttpOnly
X-SourceFiles: =?UTF-8?B?RDpcMDYgUHJvamVjdEphZGVcMDIgU2FuZGJveFwwNCBDbGVhbnVwIEFQSSBBY2NvdW50XENsZWFudXBBUElBY2NvdW50XFRva2Vu?=
X-Powered-By: ASP.NET
Date: Sun, 02 Mar 2014 15:22:57 GMT

{"access_token":"hM_60CprAm6DqCe7qgte1vsnih2d4j1Uy_FDlgoPkEgS_4u0__4lk5KNd0XysTktOfwMw4ffH3uaRmNaFObVnEY3yWS70hio03azUbCrFKk0VNgj31Y0_zLrd-J0ScZ4vzLdtw7KAXtNfcYySKk1EFtJRB4yYcqvobwORC3eu1VHyYInqy7kBgIhAZYE_NZ3zQrrGerZjy__zCuDdRtXO-klkFtg3dONq7cMP_TBi6xLmBjhXlhzUTKGzOrofijlkyMNHF1rx0CgWjhqEx2rJU8Hakq4Bac1pCqoLaYm91DRSrYO--ff4GWlP5wLeqZAhHIA7t17e2pyZXrUT7V1ExBeCnGkWbWoR8Y-QN8ocT7Q3xjydFd4uWSQD5B-Z1bC-nLpUrtkOGZiukl6J3aCJOqeidY6MEM4TMaJZlIp-Oc","token_type":"bearer","expires_in":1209599,"userName":"Alice",".issued":"Sun, 02 Mar 2014 15:22:57 GMT",".expires":"Sun, 16 Mar 2014 15:22:57 GMT"}

我怀疑json解析器需要一种不同的消息格式。我不希望更改webapi站点,所以我可能需要更改客户端实现。我不知道我需要改变什么或者我必须要去哪里。

3 个答案:

答案 0 :(得分:8)

经过一番激烈的谷歌搜索后,我的代码工作正常。

我做的第一件事就是添加了一个额外的类来存储令牌。

class TokenResponseModel
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("expires_in")]
    public int ExpiresIn { get; set; }

    [JsonProperty("userName")]
    public string Username { get; set; }

    [JsonProperty(".issued")]
    public string IssuedAt { get; set; }

    [JsonProperty(".expires")]
    public string ExpiresAt { get; set; }
}

之后我将代码更改为以下代码。

static internal async Task<TokenResponseModel> GetBearerToken(string siteUrl, string Username, string Password)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(siteUrl);
    client.DefaultRequestHeaders.Accept.Clear();

    HttpContent requestContent = new StringContent("grant_type=password&username=" + Username + "&password=" + Password, Encoding.UTF8, "application/x-www-form-urlencoded");

    HttpResponseMessage responseMessage = await client.PostAsync("Token", requestContent);

    if (responseMessage.IsSuccessStatusCode)
    {
        string jsonMessage;
        using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
        {
            jsonMessage = new StreamReader(responseStream).ReadToEnd();
        }

        TokenResponseModel tokenResponse = (TokenResponseModel)JsonConvert.DeserializeObject(jsonMessage, typeof(TokenResponseModel));

        return tokenResponse;
    }
    else
    {
        return null;
    }
}

我现在可以从客户端的WebAPI 2站点获取持有者令牌,以便将其添加到将来的请求中。我希望它对其他人有帮助。

答案 1 :(得分:3)

另一种方法是:

TokenResponseModel tokenResponse = await response.Content.ReadAsAsync<TokenResponseModel>();

答案 2 :(得分:0)

要反序列化示例的对象错误

Json String

registered_name

vs中的特殊粘贴

import { Injectable } from '@angular/core';
import platform from 'platform';
@Injectable({
  providedIn: 'root'
})
export class DeviceService {
  get isAndroid(): boolean {
    if (navigator.userAgent.match(/Android/i)) {
      return true;
    }
    return false;
  }
  get isIOS(): boolean {
    return platform.os.family === 'iOS';
  }
  get isMobile(): boolean {
    return this.isAndroid || this.isIOS;
  }
  get isSidebarEnabled(): boolean {
    return !this.isMobile;
  }
  get isMobileDevice(): boolean {
    return this.isMobile;
  }
}

代码在这里:

{
 "status":{"code":"00","description":"Success"},
 "data":{"userId":"PPP","accessToken":"123131321"}
}