Click here to Skip to main content
14,265,962 members

How to pass custom information with JSON Web Token in asp.net web api

Ajay_Patel asked:

Open original thread
Hey there,

I need to pass some custom information with my JSON Web Token in asp.net web api.
Right now it only returns access_token,expires_in and token_type properties but I need to pass some additional information along with those properties like user_name,user_roles etc. I now the json token contains these information in encoded format but as my api will be consume by other platforms like android and ios so, I need to pass these information in plain text format.
Currently I am using following code to pass custom information with JSON token but I am getting same response as before.

Dictionary<string,> d = new Dictionary<string,>();
d.Add("user_name", user.UserName);
var props = new AuthenticationProperties(d);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
var ticket = new AuthenticationTicket(oAuthIdentity, props);
context.Validated(ticket);

Below is the response of my web api:-

{
access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1laWQiOiIxNzViNzAxNC03M2YwLTQzNjctOGE0NC1iNmMxZjczNjc1MzAiLCJ1bmlxdWVfbmFtZSI6IkFkbWluIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9hY2Nlc3Njb250cm9sc2VydmljZS8yMDEwLzA3L2NsYWltcy9pZGVudGl0eXByb3ZpZGVyIjoiQVNQLk5FVCBJZGVudGl0eSIsIkFzcE5ldC5JZGVudGl0eS5TZWN1cml0eVN0YW1wIjoiYjdjMzQ5MmEtMDRmZC00OTQwLThjZDktYjEzMWRmNjFmMzIyIiwicm9sZSI6WyJVc2VyIiwiUHVibGlzaGVyIiwiQWRtaW4iXSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1OTgyMiIsImF1ZCI6IjEyMzQ1Njc4IiwiZXhwIjoxNDc2NDUzNzI3LCJuYmYiOjE0NDUzNDk3Mjd9.wxstrUpUZSD1v5t1-F5_DVBrqpD7Yxq5Tsx6bnugVYQ"
token_type: "bearer"
expires_in: 31103999
}
Tags: .NET4.5, JWT, MVC4, WebAPI

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100