[英]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.
我使用 OAuthBearerAuthentication 编写了以下代码,但无法返回更多数据。 I am only getting "token", "token-type" and "expires in".
我只得到“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);
}
}
I require more information about the user.我需要有关用户的更多信息。 For example, I have a tbl_user field in the database.
例如,我在数据库中有一个 tbl_user 字段。 Can I include other information about the user to return, other than "access_token", "token_type" and "expires_in"?
除了“access_token”、“token_type”和“expires_in”之外,我是否可以包含有关要返回的用户的其他信息? If not, how can I get the user in the API based on the access_token?
如果没有,如何根据access_token获取API中的用户?
Any help will be highly appreciated!任何帮助将不胜感激!
I just create a dictionary and save the username and userid.我只是创建一个字典并保存用户名和用户 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.