使用 AspNet.Security.OpenIdConnect.Server 的中央授权和身份验证端点 (OIDC)

Central Authorization & Authentication Endpoint Using AspNet.Security.OpenIdConnect.Server (OIDC)

我正在使用 Visual Studio 2015 Enterprise Update 1 和 ASP.NET 5 rc1-final 来构建一个端点,该端点同时发布和使用 JWT 令牌,如详细所述 。在这种方法中,我们只有一个项目 'does it all' - 该项目使用 OIDC 颁发令牌,使用 JWT 持有者身份验证来验证它们,然后使用 Authorize 属性保护对各种控制器的访问 - 所有这些都在同一个项目中。

现在我们想通过创建一个 颁发和验证令牌的 OIDC 授权和身份验证端点来重构此解决方案。然后,我们需要 'n' 依赖于该 OIDC 端点的其他端点作为验证令牌的中央机构。这将使我们能够在我们不断增长的服务 backbone 上建立更多端点,而无需将授权和身份验证编码到每个端点中。

虽然我了解如何配置 OIDC 以从一个端点颁发令牌,但我并不完全清楚如何将我的另一个端点指向 OIDC 端点以进行令牌身份验证。目前 JWT 身份验证和 OIDC 在中间件 'Configure' 方法中同时配置,所以我猜也许在所有从属站点上我会有一小段代码调用 app.UseJwtBearerAuthentication 只是将 JWT 中间件指向OIDC 端点?如果是这种情况,使用 OIDC 允许 IdentityModel 使用 HTTP 的 app.UseJwtBearerAuthentication 仍然会发生一些神奇的事情,所以我不清楚我是否也需要在从属服务器上使用它。

任何有关如何建立单个 OIDC 授权和身份验证端点然后让 'n' 从属端点指向该端点以对 JWT 令牌进行身份验证的任何建议,我们将不胜感激。

将资源服务器角色(即API)与授权服务器角色分开对于 ASOS 来说绝对是可能的。

选择JWT令牌(而不是默认的加密令牌)时,您需要通过调用ticket.SetResources正确地将受众添加到身份验证机票中,因此JWT访问令牌获得适当的[= = = 13=] 声明,包含与您的资源服务器关联的标识符(即 API):

public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext context) {
    var identity = new ClaimsIdentity(context.Options.AuthenticationScheme);
    identity.AddClaim(ClaimTypes.NameIdentifier, "[unique identifier]");

    var ticket = new AuthenticationTicket(
        new ClaimsPrincipal(identity),
        new AuthenticationProperties(),
        context.Options.AuthenticationScheme);

    // Call SetResources with the list of resource servers
    // the access token should be issued for.
    ticket.SetResources("resource_server_1");

    // Call SetScopes with the list of scopes you want to grant.
    ticket.SetScopes("profile", "offline_access");

    context.Validate(ticket);

    return Task.FromResult(0);
}     

在您的 API 应用程序中,您只需使用授权服务器中使用的标识符设置 options.Audience 属性,它应该可以工作:

app.UseJwtBearerAuthentication(new JwtBearerOptions {
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    Audience = "resource_server_1",
    Authority = "http://localhost:61854"
});

I would have a small piece of code in calling app.UseJwtBearerAuthentication simply pointing the JWT middleware to the OIDC endpoint? If this is the case there's still a bit of magic taking place with the app.UseJwtBearerAuthentication that uses OIDC to allow IdentityModel to use HTTP, so I'm not clear if I would need this on the subordinate servers also.

JWT 承载中间件自动从 options.Authority 属性、by making an HTTP call to the configuration metadata endpoint 中提到的授权服务器检索用于签署访问令牌的加密密钥:您不必配置任何东西,即使 API 项目与授权服务器应用程序分离