如何在 .net 核心中创建由 Auth0 验证的自己的访问令牌

How to create Own Access Token validated by Auth0 in .net core

我有一个现有系统,想利用 Auth0(仍在考虑)。

上下文:

我有自己的登录屏幕,每个客户端都非常动态(白色标签)。所以我不想使用 Auth0 登录屏幕。

我有自己的用户和租户数据库,所以暂时不想使用 Auth0 用户数据库。

所以我的想法是在我的后端登录后验证用户凭据,并为客户端创建 return 的访问令牌。

我希望该访问令牌由 Auth0 验证。

问题:

1) 可能是最重要的问题。应该使用 Auth0 还是坚持使用原生 jwt

2) 有没有一种方法可以在我的后端创建一个可以由 Auth0 验证的有效访问令牌。

堆栈:

我正在使用 vue.js 作为我的前端 SPA。

我正在使用 .net core 作为我的后端。

Probably most important question. Should go with Auth0 or just stick with native jwt's

这取决于您是否要启用在线身份提供商的功能,例如单点登录。如果没有,请继续使用当前版本,因为您不需要 Auth0 用户数据库。

Is there a way I can create a valid access token in my backend which can be validated by Auth0.

由于您的方案是在前端应用程序中收集用户名和密码,并传递给后端 .net 应用程序以验证凭据并创建令牌。您可以在 Auth0 中 implement the Resource Owner Password Grant 创建访问令牌以访问受 Auth0 保护的资源,但如文档所示:

You should use this flow only if the following apply:

  • The application is absolutely trusted with the user's credentials. For Single-Page Apps and Native/Mobile Apps, we recommend using web flows instead.
  • Using a redirect-based flow is not possible. If this is not the case and redirects are possible in your application, you should use the Authorization Code Flow instead.

因此不推荐您的方案。如果你需要SSO之类的功能,又想使用Auth0,建议直接在vue应用中使用Auth0来管理你的用户和角色:

https://auth0.com/docs/quickstart/spa/vuejs/01-login

Auth0登录后,如果需要可以将用户映射到本地数据库用户进行具体管理。

另一种方法是使用 Client Credential flow 。对于这种情况,典型的身份验证方案(如用户名 + 密码或社交登录)没有意义。相反,M2M 应用程序使用 Client Credentials Flow ,您的后端应用程序将对应用程序而不是用户进行身份验证和授权。如果您想为特定用户获取访问令牌以访问受保护的资源,则不适合。