【问题标题】:Unable to show additional properties in token response无法在令牌响应中显示其他属性
【发布时间】: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


【解决方案1】:

也将以下方法添加到您的CustomOAuthProvider(源自OAuthAuthorizationServerProvider):

   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);
    }

【讨论】:

  • 很高兴它有帮助!如果它是您问题的正确答案,请将其标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多