IdentityServer4 实践- 使用客户端凭据保护API

 

 

1.1-引用 IdentityServer4 

1.2-创建 Config.cs 配置类

 1 using IdentityServer4.Models;
 2 
 3 namespace IDS4.Core.ClientCredentials
 4 {
 5     /// <summary>
 6     /// 配置文件(使用客户端凭据)
 7     /// </summary>
 8     public static class Config
 9     {
10         private const string api_scope_one = "api1";
11 
12         public static IEnumerable<ApiScope> ApiScopes =>
13                                                new List<ApiScope>
14                                                {
15                                                     new ApiScope(api_scope_one, "第一个API")
16                                                };
17 
18         public static IEnumerable<Client> Clients =>
19                                                 new List<Client>
20                                                 {
21                                                     new Client
22                                                     {
23                                                         //可以将 ClientId 和 ClientSecret 视为应用程序本身的登录名和密码
24                                                         ClientId = "client",
25 
26                                                         // 没有交互式用户,使用 clientid/secret 进行身份验证
27                                                         AllowedGrantTypes = GrantTypes.ClientCredentials,
28 
29                                                         // 用于身份验证的密钥
30                                                         ClientSecrets =
31                                                         {
32                                                             new Secret("secret".Sha256())
33                                                         },
34 
35                                                         // 客户端有权访问的范围
36                                                         AllowedScopes = { api_scope_one }
37                                                     }
38                                                 };
39 
40     }
41 }
View Code

2-创建身份服务器( Identity Server)IDS4.Server

PS:使用站点空模板

IdentityServer4 实践- 使用客户端凭据保护API

 

 

2.1-引用IDS4.Core

 

2.2-配置ConfigService

1 builder.Services.AddIdentityServer()
2     // 这仅适用于没有证书可以使用的开发场景。
3     .AddDeveloperSigningCredential()
4     .AddInMemoryApiScopes(IDS4.Core.ClientCredentials.Config.ApiScopes)
5     .AddInMemoryClients(IDS4.Core.ClientCredentials.Config.Clients);
View Code

相关文章: