我可以使用 Identity Server 4 预验证我的 Angular 客户端应用程序以调用我本地托管的 .Net Core WebAPI

Can I pre-authenticate my Angular Client Application using Identity Server 4 to call my locally hosted .Net Core WebAPI

我有一个 Angular 客户端应用程序,它调用托管在同一个盒子上的 .Net Core WebAPI。它在单独的 WebAPI.OAuth 应用程序中使用 Identity Server 4 进行身份验证。

我的解决方案在一个独立的 Raspberry Pi Kiosk 上运行,因此客户端应用程序和 WebAPI 之间的大部分时间交互都在同一个盒子上。 WebAPI 驱动一些用户可以与之交互的硬件。

使用 Identity Server 4 的原因是可以从另一个网络位置访问 WebAPI 以监视 API.

发生的情况

我的问题是:我可以预先验证本地 Angular 客户端应用程序以便用户不必登录吗?由于 Identity Server 4 有很多选项,我需要对我的方法进行一些指导,而我只需要最简单的。

您可以使用资源所有者身份验证流程,它允许您代表用户自动获取令牌。

当您的 angular 应用程序启动时,它会使用 resource owner password 流程向您的身份服务器发出授权请求,并提供 client_id client_secretusernamepasswordgrant_type 获得访问令牌后,您可以将其存储在本地并在与您的 WebAPI 交互时重用它。 授权请求看起来像

POST /connect/token HTTP/1.1
Host: <identity server host>
Content-Type: application/x-www-form-urlencoded

client_id=<client_id>&client_secret= 
<client_secret>&grant_type=password&username=<username>&password=<password>

我认为您可以在这里使用很多 OAuth 2.0 流程。您可以阅读所有这些内容,here。尽管文章来自 auth0,但 IdentityServer 4 支持大部分(如果不是全部)这些流程。

您似乎在尝试对应用程序(或应用程序所在的物联网设备 运行)而不是用户本身进行身份验证。在这种情况下,我会推荐授权代码流程。对于这种方法,必须发出两个 HTTP 请求。第一个看起来像:

GET /connect/authorize?
    client_id=client1&
    scope=openid email api1&
    response_type=id_token token&
    redirect_uri=https://myapp/callback&
    state=abc&
    nonce=xyz

收到授权码后,您可以再次请求从以下端点获取 JWT 令牌。

POST /connect/token
CONTENT-TYPE application/x-www-form-urlencoded

    client_id=client1&
    client_secret=secret&
    grant_type=authorization_code&
    code=hdh922&
    redirect_uri=https://myapp.com/callback

您可以详细阅读 IdentityServer 4 的授权和令牌端点,here and here