简体   繁体   中英

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

Is there any way to return more data to the client with the bearer token? I have written the below code using OAuthBearerAuthentication but unable to return more data. I am only getting "token", "token-type" and "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);
            }
        }

I require more information about the user. For example, I have a tbl_user field in the database. Can I include other information about the user to return, other than "access_token", "token_type" and "expires_in"? If not, how can I get the user in the API based on the access_token?

Any help will be highly appreciated!

I just create a dictionary and save the username and userid.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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