在 IdentityServer4入门一 我们准备好了一个认证的服务端,这里做一个需要保护的API服务
首先,向解决方案新增一个项目。我们同样使用入门一的广式新增一个asp.net core Web程序(模型视图控制器)
同样的将端口修改一下,API的端口我们使用44301。打开Properties\launchSettings.json文件
利用nuget安装引用
Microsoft.AspNetCore.Authentication.JwtBearer
新增控制器
在controlers目录下新增“API控制器-空”,名为:IdentityController
[Route("api/[controller]")] [ApiController] public class IdentityController : ControllerBase { [HttpGet] public IActionResult Get() { return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); } }
修改startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddAuthentication("Bearer") .AddJwtBearer("Bearer", options => { options.Authority = "https://localhost:44300"; options.RequireHttpsMetadata = false; options.Audience = "api1"; }); }
好了,现在试试调试运行,并在地址栏录入
https://localhost:44301/api/identity
原文:https://www.cnblogs.com/kevin-Y/p/11684808.html