IdentityServer4 作为 Web API

IdentityServer4 as Web API

我是 IdentityServer4 的新手。现在我尝试通过 JWT 授权实现微服务(通过将每个请求传递给 API)。因此,架构是原始的——IDS4 服务器作为身份验证服务器,ASP.NET Identity + MSSQL 用于客户端数据使用和存储,其他服务使用身份验证服务进行令牌验证。 所以,我看了很多文章,但找到了 none 个示例,我可以在其中自定义 IDS4 行为。 比如我要client调用AuthorizeByLogin(AuthorizeView model) API方法,在IDS4项目中实现,其中model是2个字段的对象:Username, Password。在这种方法中,我想检查数据库中的用户并生成 access_token,它被传递给客户端以使用受保护的 API。 但是没有任何示例如何执行此操作(调用 API 方法,传递对象并接收令牌)。大部分说 "use */connect/token for this".

谁能给我一个很好实现这种方式的代码例子?或者告诉我应该实现哪些接口并正确传递给 ASP.NET Core app for Authentication Web API + IdentityServer4 realization?

中的服务

谢谢。

您可以在 Identity Server 4 docs 中找到大量快速入门示例。另外据我了解,您想要使用 ResourceOwnerCredentials 授权类型。如果不重新实现大部分 Identity Server 4,您将无法轻松修改颁发令牌的端点,但您可以实现 IResourceOwnerPasswordValidator 接口并使用适当的允许类型设置客户端:

    new Client
    {
        ClientId = "your_client",
        AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

        ClientSecrets =
        {
            new Secret("your_password".Sha256())
        },
        AllowedScopes = { ... }
    }

之后,客户端可以通过提供给定用户的用户名和密码以及他们自己的凭据来调用 connect\token 端点:

var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
{
    Address = disco.TokenEndpoint,
    ClientId = "ro.client",
    ClientSecret = "secret",

    UserName = "alice",
    Password = "password",
    Scope = "api1"
});