一、Client Credentials实例
①准备
安装模板,请打开控制台窗口,然后键入以下命令:
dotnet new -i IdentityServer4.Templates
安装后,会出现这么几个identityserver模板
我们这里选择将信息放在内存里面的In-Memory Stores and Test Users模板
②创建In-Memory Stores and Test Users模板项目
dotnet new is4inmem name--myService
在config文件中会生成几个Client类别,本案例就试用
然后我们将服务的启动项端口设为5000
③创建客户端
编写一个请求访问令牌的客户端,创建一个控制台应用设置启动端口为5002
添加IdentityModel包
从元数据中读取实际的端点地址
// discover endpoints from metadata var client = new HttpClient(); var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000"); if (disco.IsError) { Console.WriteLine(disco.Error); return; }
接下来,您可以使用发现文档中的信息向IdentityServer请求令牌以访问api1
// request token var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = disco.TokenEndpoint, ClientId = "client", ClientSecret = "secret", Scope = "api1" }); if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); return; } Console.WriteLine(tokenResponse.Json);
将访问令牌发送到API,访问api
// call api var apiClient = new HttpClient(); apiClient.SetBearerToken(tokenResponse.AccessToken); var response = await apiClient.GetAsync("http://localhost:5001/identity"); if (!response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode); } else { var content = await response.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content)); }
④定义API资源
创建一个api项目,设置启动端口为5001
添加一个名为的新类IdentityController
[Route("identity")] [Authorize] public class IdentityController : ControllerBase { [HttpGet] public IActionResult Get() { return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); } }
然后再控制器上面加上权限访问[Authorize]
最后一步是将身份验证服务添加到DI(依赖注入),并将身份验证中间件添加到管道。这些将:
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddAuthentication("Bearer") .AddJwtBearer("Bearer", options => { options.Authority = "http://localhost:5000"; options.RequireHttpsMetadata = false; options.Audience = "api1"; }); } public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
AddAuthentication
将身份验证服务添加到DI并配置Bearer
为默认方案。UseAuthentication
将身份验证中间件添加到管道中,以便对主机的每次调用都将自动执行身份验证。UseAuthorization
添加了授权中间件,以确保匿名客户端无法访问我们的API端点。http://localhost:5001/identity
在浏览器上导航至控制器应返回401状态代码。这意味着您的API需要凭据,并且现在受IdentityServer保护。
github代码地址:https://github.com/LGXQWER/identityServerDEMO
参考文献:https://identityserver4.readthedocs.io/en/latest/quickstarts/1_client_credentials.html
原文:https://www.cnblogs.com/liguix/p/12727458.html