Reputation: 27
I am using Web Api 2 and implemented custom token based authentication. It is working fine but I want to get few extra properties values in response. Even though I have added new claims and also added new properties to get their values in response but I am still get only three values in response which are 'access_token', "token_type" and "expires_in". How can I get more values in response. This is my code:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
if(context.UserName == "user" && context.Password=="user")
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim(ClaimTypes.Role, "Administrators"));
identity.AddClaim(new Claim("MyClaim", "I don't know"));
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{ "name", "John" },
{ "surname", "Smith" },
{ "age", "40" },
{ "gender", "Male" }
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
}
else
{
context.SetError("Invalid_Grant", "Provided username and password is incorrect");
return;
}
}
This is the output I am getting
{
"access_token": "xxxxx", "token_type": "bearer", "expires_in": 86399 }
Upvotes: 1
Views: 1232
Reputation: 22555
the additional claims and properties are not supposed to appear as additional fields in the response, but are encoded in the access_token itself. If you're using JWT (JSON Web Tokens) you can view the contents of the generated token at https://jwt.io/ Just paste your token into the left window and see the decoded token including all your claims on the right side.
Upvotes: 0