1、创建一个ASP.NET Core空项目
2、创建一个Config类
using IdentityServer4.Models; using System.Collections.Generic; namespace Sample { public static class Config { public static IEnumerable<ApiScope> ApiScopes => new ApiScope[] { new ApiScope{Name="sample_api",DisplayName="Sample Api"}, new ApiScope{Name="scope2",DisplayName="Scope"} }; public static IEnumerable<Client> Clients => new Client[] { new Client { ClientId="sample_Client", ClientName="Client Credentials Client", //客户端名称 AllowedGrantTypes=GrantTypes.ClientCredentials,//客户端验证(验证模式) ClientSecrets={new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256())}, AllowedScopes={ "sample_api" } //访问资源范围 } }; } }
3、配置服务和管道,IdentityServer4采用的中间件形式,必须进行管道配置
1)添加服务
public void ConfigureServices(IServiceCollection services) { var builder= services.AddIdentityServer();//添加服务 builder.AddDeveloperSigningCredential();//配置开发验证模式 builder.AddInMemoryApiScopes(Config.ApiScopes);//配置内存API资源形式 builder.AddInMemoryClients(Config.Clients);//配置内存客户端访问形式 }
2)注册应用
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseIdentityServer(); }
4、测试https://localhost:5001/.well-known/openid-configuration进行GET请求,查看
2)使用https://localhost:5001/connect/token获取token
3)token为jwt加密,解析查看
原文:https://www.cnblogs.com/CelonY/p/14284389.html