1

I have the following code to create a token:

 var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
        using (var db = new mobileappEntities())
        {
            var user =
            db.tbl.Where(u => u.Mobile_Number == context.UserName && u.Password == context.Password).FirstOrDefault();
            if (user != null)
            {
                var props = new AuthenticationProperties(new Dictionary<string, string>
                        {
                            {
                                "userName", user.First_Name
                            },
                            {
                                 "Student_PId", user.Student_PId.ToString()
                            }
                         });
                var ticket = new AuthenticationTicket(identity, props);
                context.Validated(ticket);
            }
            else
            {
                context.SetError("Provided username and password is incorrect");
                context.Rejected();
            }
        }

And I am getting following response:

{
"access_token": "f2q-bPTQT_d6op8a73rR6Xvc7iie4rLXL-qill2pnTsbKdzkYPDA6a1DpKM_HB--FKf9P4uOfl_4TLxQpvy1TIGKSeF2kR6fHmpTsqKBBe4uKwrwrZswQyZcFwgFFQ2qgqD4aWJP5_mi2seRviHaFLie6fGhWmFBIGQgVBSalW0HVLKe4Gtg7-1rqcI0jRQTOr1Cgz5pEA3WA1-F-6Z9ui-pG6Nv8ynCI5vjUyA2loZcrmeo5CbvlCNNGCoAPB86",
"token_type": "bearer",
"expires_in": 1199
}

But I want a response with userID

{
"access_token": "f2q-bPTQT_d6op8a73rR6Xvc7iie4rLXL-qill2pnTsbKdzkYPDA6a1DpKM_HB--FKf9P4uOfl_4TLxQpvy1TIGKSeF2kR6fHmpTsqKBBe4uKwrwrZswQyZcFwgFFQ2qgqD4aWJP5_mi2seRviHaFLie6fGhWmFBIGQgVBSalW0HVLKe4Gtg7-1rqcI0jRQTOr1Cgz5pEA3WA1-F-6Z9ui-pG6Nv8ynCI5vjUyA2loZcrmeo5CbvlCNNGCoAPB86",
"token_type": "bearer",
"expires_in": 9875,
"UserID": 1234
}

How can I achieve this?

jps
  • 20,041
  • 15
  • 75
  • 79
  • If an answer has solved your question please consider [accepting it](https://stackoverflow.com/help/someone-answers) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – jps Feb 26 '19 at 11:16

1 Answers1

1

Add the following method to your CustomOAuthProvider (derived from OAuthAuthorizationServerProvider) too:

   public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
        {
            context.AdditionalResponseParameters.Add(property.Key, property.Value);
        }
        return Task.FromResult<object>(null);
    }
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20