Return 使用 OAuth 令牌生成 WebApi 向带有不记名令牌的客户端发送更多数据
Return more data to the client with the bearer token using OAuth token generation WebApi
有什么方法可以 return 使用不记名令牌向客户端发送更多数据吗?
我使用 OAuthBearerAuthentication 编写了以下代码,但无法 return 更多数据。我只得到“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”之外,我能否将有关用户的其他信息添加到 return?如果不是,我如何根据 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() }
});
有什么方法可以 return 使用不记名令牌向客户端发送更多数据吗? 我使用 OAuthBearerAuthentication 编写了以下代码,但无法 return 更多数据。我只得到“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”之外,我能否将有关用户的其他信息添加到 return?如果不是,我如何根据 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() }
});