0

Our GET request returns the following response:

{
    "access_token": "pi7ID4bsZIC9yN ... 76gnblw",
    "token_type": "bearer",
    "expires_in": 1209599,
    "userName": "super0",
    "userRoles": "["member", "admin"]",
    ".issued": "Tue, 04 Feb 2014 05:07:51 GMT",
    ".expires": "Tue, 18 Feb 2014 05:07:51 GMT"
}

The problem is that AngularJS parses it into the following object.

data: Object
    .expires: "Tue, 18 Feb 2014 05:07:51 GMT"
    .issued: "Tue, 04 Feb 2014 05:07:51 GMT"
    access_token: "pi7ID4bsZIC9yN ... 76gnblw"
    expires_in: 1209599
    token_type: "bearer"
    userName: "super0"
    userRoles: "["member", "admin"]"
    __proto__: Object

We need "userRoles" to parse into a JavaScript array not a string as shown. How can we do this?

1 Answer 1

1

That's not valid JSON. The array should not be quoted.

It should look like this instead:

{
    "access_token": "pi7ID4bsZIC9yN ... 76gnblw",
    "token_type": "bearer",
    "expires_in": 1209599,
    "userName": "super0",
    "userRoles": ["member", "admin"],
    ".issued": "Tue, 04 Feb 2014 05:07:51 GMT",
    ".expires": "Tue, 18 Feb 2014 05:07:51 GMT"
}
Sign up to request clarification or add additional context in comments.

4 Comments

Aha. That makes sense. The problem then is that I am unable to return an unquoted array, because C# creates the JSON GET response from a Dictionary<string,string>. The JSON property value must be a string, and I don't know how to include square brackets outside the quotes data.Add("userRoles", "['admin', 'member']");
Can you make that a Dictionary<string, object> instead, so the value for the userRoles key can be an Array (or IEnumerable of some sort)? Then, it should serialize correctly.
I could try that but am hesitant. ASP.NET Identity's AuthenticationProperties() constructor takes a Dictionary<string,string>. Implementing our own AuthenticationProperties class might do the trick. That's going to require some work, though. Hmm.
I will probably just pass the userRoles as CSV instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.