无法在令牌响应中显示其他属性

时间:2019-02-26 07:34:59

标签: c# asp.net-mvc asp.net-web-api access-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();
            }
        }

我得到以下回应:

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

但是我想要一个带有userID的响应

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

我该如何实现?

1 个答案:

答案 0 :(得分:0)

也将以下方法添加到您的arr_downloadName[chekced_download] = document.getElementsByName("check")[i].value; (源自CustomOAuthProvider):

OAuthAuthorizationServerProvider