【发布时间】:2019-07-19 16:45:49
【问题描述】:
我有以下代码来创建令牌:
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
}
但我想要一个带有用户 ID 的响应
{
"access_token": "f2q-bPTQT_d6op8a73rR6Xvc7iie4rLXL-qill2pnTsbKdzkYPDA6a1DpKM_HB--FKf9P4uOfl_4TLxQpvy1TIGKSeF2kR6fHmpTsqKBBe4uKwrwrZswQyZcFwgFFQ2qgqD4aWJP5_mi2seRviHaFLie6fGhWmFBIGQgVBSalW0HVLKe4Gtg7-1rqcI0jRQTOr1Cgz5pEA3WA1-F-6Z9ui-pG6Nv8ynCI5vjUyA2loZcrmeo5CbvlCNNGCoAPB86",
"token_type": "bearer",
"expires_in": 9875,
"UserID": 1234
}
我怎样才能做到这一点?
【问题讨论】:
-
如果答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您找到了解决方案,并为回答者和您自己赢得了一些声誉。
标签: c# asp.net-mvc asp.net-web-api access-token