【发布时间】:2016-09-16 16:04:19
【问题描述】:
我正在使用不记名令牌为 OAuth 2 配置 AspNet.Identity,并且我看到了多个实现 OAuthAuthorizationServerProvider.GrantRefreshToken 方法的示例,其中作者演示了向 new ClaimsIdentity 添加声明的能力,如下所示。
我试图在我的单一服务器(即我的 Web API 项目既是授权服务器 + 资源服务器)的上下文中理解这一点,我可能会在以后将其拆分为单独的服务器,如果需要的话。
public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
var currentClient = context.ClientId;
if (originalClient != currentClient)
{
context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
return Task.FromResult<object>(null);
}
// Change auth ticket for refresh token requests
var newIdentity = new ClaimsIdentity(context.Ticket.Identity);
// CONSIDER: I don't know why you would add a claim here, but here's an example.
//var newClaim = newIdentity.Claims.Where(c => c.Type == "newClaim").FirstOrDefault();
//if (newClaim != null)
//{
// newIdentity.RemoveClaim(newClaim);
//}
//newIdentity.AddClaim(new Claim("newClaim", "newValue"));
var newTicket = new AuthenticationTicket(newIdentity, context.Ticket.Properties);
context.Validated(newTicket);
return Task.FromResult<object>(null);
}
“应用程序必须调用 context.Validated 以指示授权服务器中间件根据这些声明和属性发出 访问令牌。”
我不明白这一点。我以为我们分发的是刷新令牌,而不是访问令牌。
此外,“对 context.Validated 的调用可能会被赋予不同的 AuthenticationTicket 或 ClaimsIdentity 以控制哪些信息从刷新令牌流向访问令牌。”
我认为所有声明都存储在我的签名和加密访问令牌中,该令牌以Authorization: Bearer XXXXXX 传递。但是,我对 ClaimsIdentity 和 AuthenticationTicket 与我的 OAuth 2.0 流程中的任何内容的实际关系有一个微妙的了解。
我的最佳猜测是GrantRefreshToken 需要获取已经过身份验证和授权的身份 (context.Ticket.Identity),并通过调用 context.Validated 来验证是否应该向其添加刷新令牌。
【问题讨论】:
-
是的,您发送了一个刷新令牌,但基于此刷新令牌,服务器会发出访问令牌。因此,如果您不调用 Validated,您最终将不会获得访问令牌。
The call to context.Validated may be given a different AuthenticationTicket or ClaimsIdentity in order to control which information flows from the refresh token to the access token. The default behavior when using the OAuthAuthorizationServerProvider is to flow information from the refresh token to the access token unmodified.因此,默认情况下不会在此处添加进一步的声明 --> 未修改。我是这样理解的
标签: asp.net asp.net-web-api oauth oauth-2.0 asp.net-web-api2