【发布时间】:2023-03-15 10:53:01
【问题描述】:
我正在尝试了解 MVC 5 中单页应用程序模板中新的 OWIN Bearer Token 身份验证过程。如果我错了,请纠正我,对于 OAuth 密码客户端身份验证流程,Bearer Token 身份验证通过检查 http Bearer 访问令牌代码的授权请求标头,用于查看请求是否经过身份验证,它不依赖 cookie 来检查特定请求是否经过身份验证。
根据这篇文章:
OWIN Bearer Token Authentication with Web API Sample
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (IdentityManager identityManager = _identityManagerFactory.CreateStoreManager())
{
if (!await identityManager.Passwords.CheckPasswordAsync(context.UserName, context.Password))
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
string userId = await identityManager.Logins.GetUserIdForLocalLoginAsync(context.UserName);
IEnumerable<Claim> claims = await GetClaimsAsync(identityManager, userId);
ClaimsIdentity oAuthIdentity = CreateIdentity(identityManager, claims,
context.Options.AuthenticationType);
ClaimsIdentity cookiesIdentity = CreateIdentity(identityManager, claims,
_cookieOptions.AuthenticationType);
AuthenticationProperties properties = await CreatePropertiesAsync(identityManager, userId);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
}
GrantReourceOwnerCredentials 函数不仅使用以下行组成票证: context.Validated(ticket);但它也组成一个 cookie 身份,并使用以下行将其设置为 cookie: context.Request.Context.Authentication.SignIn(cookiesIdentity);
所以我的问题是,这个函数中 cookie 的确切用途是什么? AuthenticationTicket 不应该足以用于身份验证吗?
【问题讨论】:
-
您所指的帖子链接已更改,现在是blogs.msdn.microsoft.com/webdev/2013/09/20/…
标签: asp.net-mvc-5 owin katana asp.net-web-api2