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