ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记
任務9:客戶端集成IdentityServer
新建 API 項目
dotnet new webapi --name ClientCredentialApi控制器添加驗證
using Microsoft.AspNetCore.Authorization;namespace ClientCredentialApi.Controllers {[ApiController][Route("[controller]")][Authorize]public class WeatherForecastController : ControllerBase添加驗證之后這個 API 就無法訪問,需要添加認證授權模式
因為這是客戶端,所以只需要添加 IdentityServer4.AccessTokenValidation
添加 Nuget 包之后
dotnet restore注冊服務
services.AddAuthentication("Bearer").AddIdentityServerAuthentication(options => {options.Authority = "http://localhost:5000";// 獲取授權地址options.RequireHttpsMetadata = false;options.ApiName = "api";});使用服務
app.UseAuthentication();在 Program.cs 中配置啟動端口
webBuilder.UseUrls("http://localhost:5001");啟動程序
dotnet run訪問地址
http://localhost:5001/weatherforecast返回 401,未授權
VS Code 添加另一個控制臺,啟動 IdentityServerCenter
訪問地址
http://localhost:5000/.well-known/openid-configuration獲取 token_endpoint
"token_endpoint": "http://localhost:5000/connect/token",通過 Postman 獲取 token
使用 Post 的方式訪問 token_endpoint
http://localhost:5000/connect/tokenBody 添加三個參數(參數在 IdentityServerCenter 的 Config.cs 中定義)
發送請求獲取 access_token
通過 access_token 訪問客戶端
訪問地址
http://localhost:5001/weatherforecastHeaders 添加參數 Authorization,Value 為 Bearer + access_token
返回200,授權訪問成功
課程鏈接
http://video.jessetalk.cn/course/explore
相關文章
ASP.NET Core分布式項目實戰(業務介紹,架構設計,oAuth2,IdentityServer4)--學習筆記
ASP.NET Core分布式項目實戰(課程介紹,MVP,瀑布與敏捷)--學習筆記
ASP.NET Core快速入門 -- 學習筆記匯總
總結
以上是生活随笔為你收集整理的ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5分钟看懂微服务架构下的Consul 特
- 下一篇: 在ASP.NET Core中创建基于Qu