一:初识IdentityServer4
1:添加nuget包:IdentityServer4
2:添加Config.cs文件作为IdentityServer配置文件,用于定义IdentityServer资源和客户端等。
public class Config
{
public static IEnumerable<ApiScope> ApiScopes =>
new List<ApiScope>
{
new ApiScope("api1", "My API")
};
public static IEnumerable<Client> Clients =>
new List<Client>
{
new Client
{
ClientId = "client",
// no interactive user, use the clientid/secret for authentication
AllowedGrantTypes = GrantTypes.ClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
// scopes that client has access to
AllowedScopes = { "api1" }
}
};
}
3:配置IdentityServer
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiScopes(Config.ApiScopes)
.AddInMemoryClients(Config.Clients);
4:启用
app.UseIdentityServer();
访问地址
https://localhost:44354/.well-known/openid-configuration
出现以下内容代表配置成功
5:测试接口