0

I have claims/Auth_token information, which looks like

{
    "claims": null,
    "auth_token": "ABCDEFGHIJKLMNOP==",
    "refresh_token": null,
    "auth_token_expiration": "2012-09-04T06:59:13.1343331-04:00",
    "refresh_token_expiration": "2013-05-01T06:59:13.1343331-04:00",
    "token_type": "urn:Test1:Test2:grant-type:trusted_issuer"
}
url=www.testuri.com

Using this I need to create a utility which fetches the access token of the uri using the claims information mentioned above.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
rampuriyaaa
  • 4,926
  • 10
  • 34
  • 41

2 Answers2

4

The information you are getting is JSON

You can deserialize JSON into objects with the JavaScriptSerializer class in C#.

First you'll have to build a POCO object which represents the structure of your json:

public class ResponseObj
{
    public string claims { get; set; }
    public string auth_token { get; set; }
    public string refresh_token { get; set; }
    public DateTime auth_token_expiration { get; set; }
    public DateTime refresh_token_expiration { get; set; }
    public string token_type { get; set; }
}

After that you can deserialize it like this and use the result to fetch the token:

string json = "your json string"
ResponseObj deserializedResult = new JavaScriptSerializer().Deserialize<ResponseObj>(json);

string token = deserializedResult.auth_token;

Note that now you can access all properties in the response just like the auth token. If you'll want to get the claims string you could use;

string claims = deserializedResult.claims;
middelpat
  • 2,555
  • 1
  • 20
  • 29
2

This is a JSON string

You need to create a class with properties (claims,auth_token,refresh_token...etc)

Then DeSerialize this JSON string and then you can access the token.

    public class TokenResponse
    {
     public string claims { get; set; }
     public string auth_token { get; set; }
     public string refresh_token { get; set; }
     public string auth_token_expiration { get; set; }
     public string refresh_token_expiration { get; set; }
     public string token_type { get; set; }
    }

Now deserialize JSON:

    JavaScriptSerializer js = new JavaScriptSerializer();
    var token = js.Deserialize<TokenResponse>(decodedResponse);

Now use the token:

    string authToken=token.auth_token
tango
  • 39
  • 6