【发布时间】:2020-02-03 19:18:27
【问题描述】:
我只是想理解下面的代码:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
基于这一行
context.Request.Context.Authentication.SignIn(cookiesIdentity);
看起来它将每个令牌的声明身份信息(直到令牌过期)保存在服务器(会话)上的 cookie 中的某个位置。它可能是一个字典,键作为令牌,值作为 ClaimsIdentity 的对象。
【问题讨论】:
-
我认为您误解了代码。 *OAuth 将令牌解析为用户名和密码 * - 不,用户将其凭据(用户名/密码)发送到端点以请求令牌,上面的代码验证凭据并创建令牌。
-
是的,没错。我误会了。
-
@jps 我现在已经编辑了这个问题。你能帮我更好地理解它吗?
标签: oauth-2.0 asp.net-web-api2 bearer-token