簡體   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