使用 Openiddict 进行用户身份管理和应用程序访问

Using Openiddict for user identity management and application access

我有一个 .NET Core web API,它使用 openiddict 进行身份管理,绑定到 SQL 服务器后端中的 ASP.NET 身份。许多应用程序可以通过订阅利用此 API。我有以下要求。

  1. 只有授权的应用程序才能使用 API
  2. 每个应用程序都可以选择使用 API 的身份特征来管理其应用程序的特定用户,正如 Openiddict 在 API 中实现的那样(目前授权、密码和刷新令牌流已启用) ).
  3. 所有端点都应要求应用程序位于 Oppenidict 应用程序 table 中,并且由于 API 中的多租户支持,此应用程序 ID 应可用于每个请求。
  4. 具有 [Authorize] 属性的端点必须拥有通过 Openiddict 身份模型进行身份验证的用户。

为了实现要求 (1),我是否需要实现一个自定义授权功能来检查应用程序机密,或者是否应该在 openiddict 中启用另一个流程来确保只允许授权应用程序访问 API(不考虑授权属性)?在这种情况下,用户可能未通过身份验证,但应用程序必须仍然有权访问 API.

的未授权端点

为实现外部身份提供者的要求 (2),是否可以为在 openiddict 中注册的每个应用程序配置多个秘密,以允许其用户利用 facebook 或 twitter 进行身份验证?这很重要,因为 API 需要在配置期间为每个可以访问 API 的应用程序调用 AddFacebook() (而不是 API 本身的 clientID 和密码)。因为多个应用程序每个都有自己的 facebookID 和秘密,所以我假设这只有在 openiddict 允许通过 AddFacebook() 为同一提供者类型注册多个 ID 和秘密时才有效。

要实现要求 (3),是否有内置方法从 openiddict 获取调用应用程序的应用程序 ID,就像用户已通过身份验证一样?

To implement requirement (1), would I need to implement a custom authorization function that checks for an application secret or should another flow be enabled in openiddict that takes care of ensuring only authorized applications are allowed access to the API (regardless of authorize attributes)?

从 RC3 开始,默认情况下强制执行客户端身份验证:如果您不发送与应用程序 table 中的条目相对应的 client_id,您的请求将被 OpenIddict 本身拒绝.在以前的版本中,您可以通过调用 options.RequireClientIdentification()(现在选择退出)来选择此功能。

To implement requirement (2) for external identity providers, is it possible to configure multiple secrets for each application registered within openiddict to allow their users to leverage facebook or twitter for authentication? This is important, because the API would need call AddFacebook() during configuration for each application that can access the API (not the clientID and secret of the API itself). Because multiple applications each have their own facebookID and secret, I would assume this would only work if openiddict could allow the registration of multiple Ids and secrets for the same provider type via AddFacebook() for example.

OpenIddict 和您使用的身份验证方案之间没有直接关系,所以不,您不能配置多个 Facebook 凭据 "via" OpenIddict,因为两者无关。

假设您出于多租户目的需要它,您可能需要阅读 以了解如何覆盖 ASP.NET Core 的默认选项监视器,以便您可以为身份验证处理程序提供特定于租户的选项,例如OpenIddict、Facebook、Google 和其他任何东西。

To implement requirement (3), is there a built in way to get the application ID of the calling application from openiddict like there is if the user was authenticated?

假设它是已知的(即您没有在 OpenIddict 选项中明确将客户端标识设为可选),是的。例如来自 MVC 控制器:

var result = await HttpContext.AuthenticateAsync(OpenIddictValidationDefaults.AuthenticationScheme);
Debug.Assert(result.Ticket != null, "The authentication ticket shouldn't be null.");

// The presenters list contains the authorized parties allowed to
// use the access token with your APIs. Usually, it contains a single value.
// Note: this extension requires a using for AspNet.Security.OpenIdConnect.Extensions.
var client = result.Ticket.GetPresenters().FirstOrDefault();