IdentityServer4Config.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using IdentityServer4;
  2. using IdentityServer4.Models;
  3. using Long.Common;
  4. namespace Auth
  5. {
  6. /// <summary>
  7. /// 认证配置
  8. /// </summary>
  9. public static class IdentityServer4Config
  10. {
  11. /// <summary>
  12. /// 范围
  13. /// </summary>
  14. /// <returns></returns>
  15. public static IEnumerable<ApiScope> ApiScopes()
  16. {
  17. return new List<ApiScope>
  18. {
  19. new ApiScope(IdsScope.GuestApi, "GuestAPI"),
  20. new ApiScope(IdsScope.UserApi, "UserAPI"),
  21. new ApiScope(IdsScope.AdminApi, "AdminAPI")
  22. };
  23. }
  24. /// <summary>
  25. /// 资源
  26. /// </summary>
  27. /// <returns></returns>
  28. public static IEnumerable<ApiResource> ApiResources()
  29. {
  30. return new List<ApiResource>
  31. {
  32. new ApiResource("LongApi", "Dotnet Core API")
  33. {
  34. Scopes = { IdsScope.GuestApi, IdsScope.UserApi, IdsScope.AdminApi }
  35. }
  36. };
  37. }
  38. /// <summary>
  39. /// 客户端
  40. /// </summary>
  41. /// <returns></returns>
  42. public static IEnumerable<Client> Clients()
  43. {
  44. var configurationBuilder = new ConfigurationBuilder();
  45. configurationBuilder.AddJsonFile("appsettings.json", false, true);
  46. var configuration = configurationBuilder.Build();
  47. return new List<Client>
  48. {
  49. new Client
  50. {
  51. ClientId = "guest",
  52. AllowedGrantTypes = GrantTypes.ClientCredentials,
  53. ClientSecrets =
  54. {
  55. new Secret("Long".Sha256())
  56. },
  57. AllowedScopes =
  58. {
  59. IdsScope.GuestApi,
  60. IdentityServerConstants.StandardScopes.OpenId,
  61. IdentityServerConstants.StandardScopes.OfflineAccess,
  62. IdentityServerConstants.StandardScopes.Profile
  63. },
  64. AccessTokenLifetime = 24 * 3600,
  65. AllowOfflineAccess = true,
  66. RefreshTokenExpiration = TokenExpiration.Sliding,
  67. SlidingRefreshTokenLifetime = 24 * 3600
  68. },
  69. new Client
  70. {
  71. ClientId = "user",
  72. AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
  73. ClientSecrets =
  74. {
  75. new Secret("Long".Sha256())
  76. },
  77. AllowedScopes =
  78. {
  79. IdsScope.UserApi,
  80. IdentityServerConstants.StandardScopes.OpenId,
  81. IdentityServerConstants.StandardScopes.OfflineAccess,
  82. IdentityServerConstants.StandardScopes.Profile
  83. },
  84. AccessTokenLifetime = 24 * 3600,
  85. AllowOfflineAccess = true,
  86. RefreshTokenExpiration = TokenExpiration.Sliding,
  87. SlidingRefreshTokenLifetime = 24 * 3600
  88. },
  89. new Client
  90. {
  91. ClientId = "admin",
  92. AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
  93. ClientSecrets =
  94. {
  95. new Secret("Long".Sha256())
  96. },
  97. AllowedScopes =
  98. {
  99. IdsScope.AdminApi,
  100. IdentityServerConstants.StandardScopes.OpenId,
  101. IdentityServerConstants.StandardScopes.OfflineAccess,
  102. IdentityServerConstants.StandardScopes.Profile
  103. },
  104. AccessTokenLifetime = 24 * 3600,
  105. AllowOfflineAccess = true,
  106. RefreshTokenExpiration = TokenExpiration.Sliding,
  107. SlidingRefreshTokenLifetime = 24 * 3600
  108. },
  109. new Client
  110. {
  111. ClientId = "web",
  112. //ClientName = "admin",
  113. AllowedGrantTypes = GrantTypes.Implicit,
  114. RequireConsent = false,
  115. RedirectUris = {configuration["RedirectUris"] },
  116. PostLogoutRedirectUris = {configuration["PostLogoutRedirectUris"] },
  117. ClientSecrets =
  118. {
  119. new Secret("Long".Sha256())
  120. },
  121. AllowedScopes =
  122. {
  123. IdentityServerConstants.StandardScopes.OpenId,
  124. IdentityServerConstants.StandardScopes.Profile
  125. }
  126. }
  127. };
  128. }
  129. }
  130. }