InitConfig.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using IdentityServer4;
  2. using IdentityServer4.Models;
  3. using Microsoft.Extensions.Configuration;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace QM.AuthServer
  9. {
  10. public class InitConfig
  11. {
  12. /// <summary>
  13. /// 定义哪些API将使用此IdentityServer
  14. /// </summary>
  15. /// <returns></returns>
  16. public static IEnumerable<ApiResource> GetApiResources(IConfigurationSection section)
  17. {
  18. List<ApiResource> resource = new List<ApiResource>();
  19. if (section != null)
  20. {
  21. List<ApiConfig> configs = new List<ApiConfig>();
  22. section.Bind("ApiResources", configs);
  23. foreach (var config in configs)
  24. {
  25. resource.Add(new ApiResource(config.Name, config.DisplayName) {
  26. Scopes = { config.Name }
  27. });
  28. }
  29. }
  30. return resource.ToArray();
  31. }
  32. /// <summary>
  33. /// ApiScope
  34. /// </summary>
  35. /// <param name="section"></param>
  36. /// <returns></returns>
  37. public static IEnumerable<ApiScope> GetApiScopes(IConfigurationSection section)
  38. {
  39. List<ApiScope> resource = new List<ApiScope>();
  40. if (section != null)
  41. {
  42. List<ApiConfig> configs = new List<ApiConfig>();
  43. section.Bind("ApiResources", configs);
  44. foreach (var config in configs)
  45. {
  46. resource.Add(new ApiScope(config.Name, config.DisplayName));
  47. }
  48. }
  49. return resource.ToArray();
  50. }
  51. /// <summary>
  52. /// 定义受信任的客户端
  53. /// </summary>
  54. /// <returns></returns>
  55. public static IEnumerable<Client> GetClients(IConfigurationSection section)
  56. {
  57. List<Client> clients = new List<Client>();
  58. if (section != null)
  59. {
  60. List<ClientConfig> configs = new List<ClientConfig>();
  61. section.Bind("Clients", configs);
  62. foreach (var config in configs)
  63. {
  64. Client client = new Client();
  65. client.ClientId = config.ClientId;
  66. List<Secret> clientSecrets = new List<Secret>();
  67. foreach (var secret in config.ClientSecrets)
  68. {
  69. clientSecrets.Add(new Secret(secret.Sha256()));
  70. }
  71. client.ClientSecrets = clientSecrets.ToArray();
  72. GrantTypes grantTypes = new GrantTypes();
  73. var allowedGrantTypes = grantTypes.GetType().GetProperty(config.AllowedGrantTypes);
  74. client.AllowedGrantTypes = allowedGrantTypes == null ?
  75. GrantTypes.ClientCredentials : (ICollection<string>)allowedGrantTypes.GetValue(grantTypes, null);
  76. List<string> aas = new List<string>();
  77. aas.AddRange(config.AllowedScopes);
  78. aas.Add(IdentityServerConstants.StandardScopes.OpenId);
  79. aas.Add(IdentityServerConstants.StandardScopes.Profile);
  80. client.AllowedScopes = aas.ToArray();
  81. client.AccessTokenLifetime = config.AccessTokenLifetime * 60 * 60; //(秒)
  82. //client.AlwaysSendClientClaims = true;
  83. clients.Add(client);
  84. }
  85. }
  86. return clients.ToArray();
  87. }
  88. /// <summary>
  89. /// 定义哪些IdentityResources将使用此IdentityServer
  90. /// </summary>
  91. /// <returns></returns>
  92. public static IEnumerable<IdentityResource> GetIdentityResources()
  93. {
  94. return new List<IdentityResource>
  95. {
  96. new IdentityResources.OpenId(),
  97. new IdentityResources.Profile(),
  98. };
  99. }
  100. }
  101. /// <summary>
  102. /// API配置(可数据库管理)
  103. /// </summary>
  104. public class ApiConfig
  105. {
  106. /// <summary>
  107. /// 名称
  108. /// </summary>
  109. public string Name { get; set; }
  110. /// <summary>
  111. /// 显示名
  112. /// </summary>
  113. public string DisplayName { get; set; }
  114. }
  115. /// <summary>
  116. /// 客户端配置(可数据库管理)
  117. /// </summary>
  118. public class ClientConfig
  119. {
  120. /// <summary>
  121. /// 客户端编码
  122. /// </summary>
  123. public string ClientId { get; set; }
  124. /// <summary>
  125. /// 客户端名称
  126. /// </summary>
  127. public string ClientName { get; set; }
  128. /// <summary>
  129. /// 过期时间(小时)
  130. /// </summary>
  131. public int AccessTokenLifetime { get; set; } = 8;
  132. /// <summary>
  133. /// 客户密钥
  134. /// </summary>
  135. public List<string> ClientSecrets { get; set; }
  136. /// <summary>
  137. /// 授权模式
  138. /// </summary>
  139. public string AllowedGrantTypes { get; set; }
  140. /// <summary>
  141. /// 允许的作用域
  142. /// </summary>
  143. public List<string> AllowedScopes { get; set; }
  144. }
  145. }