繁体   English   中英

使用 OAuth 令牌生成 WebApi 将更多数据返回给带有不记名令牌的客户端

[英]Return more data to the client with the bearer token using OAuth token generation WebApi

有没有办法使用不记名令牌向客户端返回更多数据? 我使用 OAuthBearerAuthentication 编写了以下代码,但无法返回更多数据。 我只得到“token”、“token-type”和“expires in”。

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            using (UserMasterRepository _repo = new UserMasterRepository())
            {
                var user = _repo.ValidateUser(context.UserName, context.Password);
                if (user == null)
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    return;
                }
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Role, (user.role_id).ToString()));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.user_name));
                identity.AddClaim(new Claim("Email", user.user_email));
                identity.AddClaim(new Claim("Phone Number", user.user_phone_no));
                context.Validated(identity);
            }
        }

我需要有关用户的更多信息。 例如,我在数据库中有一个 tbl_user 字段。 除了“access_token”、“token_type”和“expires_in”之外,我是否可以包含有关要返回的用户的其他信息? 如果没有,如何根据access_token获取API中的用户?

任何帮助将不胜感激!

我只是创建一个字典并保存用户名和用户 ID。

 if (user == null)
                    {
                        context.SetError("invalid_grant", "Provided username and password is incorrect");
                        return;
                    }                
    
                    var props =  new AuthenticationProperties(
                    new Dictionary<string, string>
                    {
                        { "user_id", user.user_id.ToString() },
                        { "user_name", user.user_name.ToString() }                    
                    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM