有没有办法在 asp.net core 3.1 Rest-API 中使用 Identity Server 功能

Is there a way to use Identity Server features in asp.net core 3.1 Rest-API

我正在一个项目中使用 Rest-API 在服务器中使用 asp.net 核心 3.1,angular 作为单页应用程序的前端。从每个客户端,用户需要提供他们的用户名和密码才能访问网络的受保护部分 API。我想使用 Identity Server 的功能来访问 ASP.NET Core Identity UserManager、RoleManager 和 SignInManagers 以确定提供的用户名和密码是否有效。

我以前从未这样做过,我试图在 Internet 上搜索相关信息,但找不到太多信息。我想要一些有关使用哪些 nuget 包以及我应该如何配置启动的帮助。

谢谢

我没有特别用 Angular 做过这个(我用的是 vue),但它基本上是相同的概念。快速 Google 搜索找到了一个 Angular 可能值得浏览的特定教程:https://fullstackmark.com/post/21/user-authentication-and-identity-with-angular-aspnet-core-and-identityserver. On the IS4 side, I would suggest starting with the Asp.Net Identity IS4 template (https://identityserver4.readthedocs.io/en/latest/quickstarts/6_aspnet_identity.html)

客户主要分为三种类型。 Official Document For Identity Clients Github link for the Official Identity Sample code

为服务器到服务器的通信定义客户端

在这种情况下,不存在交互式用户 - 服务(又名客户端)想要与 API(又名范围)通信:

public class Clients
{
    public static IEnumerable<Client> Get()
    {
        return new List<Client>
        {
            new Client
            {
                ClientId = "service.client",
                ClientSecrets = { new Secret("secret".Sha256()) },

                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = { "api1", "api2.read_only" }
            }
        };
    }
}

定义基于浏览器的 JavaScript 客户端(例如 SPA)用于用户身份验证和委派访问以及 API

此客户端使用所谓的隐式流从 JavaScript:

请求身份和访问令牌
var jsClient = new Client
{
    ClientId = "js",
    ClientName = "JavaScript Client",
    ClientUri = "http://identityserver.io",

    AllowedGrantTypes = GrantTypes.Implicit,
    AllowAccessTokensViaBrowser = true,

    RedirectUris =           { "http://localhost:7017/index.html" },
    PostLogoutRedirectUris = { "http://localhost:7017/index.html" },
    AllowedCorsOrigins =     { "http://localhost:7017" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,

        "api1", "api2.read_only"
    }
};

为用户身份验证和委托 API 访问定义服务器端 Web 应用程序(例如 MVC)

交互式服务器端(或本机 desktop/mobile)应用程序使用混合流。此流程为您提供最佳安全性,因为访问令牌仅通过后台通道调用传输(并允许您访问刷新令牌):

var mvcClient = new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",
    ClientUri = "http://identityserver.io",

    AllowedGrantTypes = GrantTypes.Hybrid,
    AllowOfflineAccess = true,
    ClientSecrets = { new Secret("secret".Sha256()) },

    RedirectUris =           { "http://localhost:21402/signin-oidc" },
    PostLogoutRedirectUris = { "http://localhost:21402/" },
    FrontChannelLogoutUri =  "http://localhost:21402/signout-oidc",

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,

        "api1", "api2.read_only"
    },
};

在 appsettings.json

中定义客户端

AddInMemoryClients 扩展方法还支持从 ASP.NET 核心配置文件添加客户端。这允许您直接从 appsettings.json 文件定义静态客户端:

"IdentityServer": {
  "IssuerUri": "urn:sso.company.com",
  "Clients": [
    {
      "Enabled": true,
      "ClientId": "local-dev",
      "ClientName": "Local Development",
      "ClientSecrets": [ { "Value": "<Insert Sha256 hash of the secret encoded as Base64 string>" } ],
      "AllowedGrantTypes": [ "implicit" ],
      "AllowedScopes": [ "openid", "profile" ],
      "RedirectUris": [ "https://localhost:5001/signin-oidc" ],
      "RequireConsent": false
    }
  ]
}

然后将配置部分传递给AddInMemoryClients method:In Startup.cs

AddInMemoryClients(configuration.GetSection("IdentityServer:Clients"))

子类别或详细客户列表:

1.客户端凭据:

2。资源所有者客户端

3。 JS OIDC 客户端

4. JS OAuth 客户端

5. MVC 混合客户端

6. MVC 隐式客户端