IdentityServer4(OAuth2.0服务)折腾笔记
以下,稱這個(gè)使用了IdentityServer4的OAuth2.0服務(wù)的項(xiàng)目稱為Passport項(xiàng)目。
組件說(shuō)明(Nuget)
Passport項(xiàng)目所需組件
項(xiàng)目以Asp.net Core Identity項(xiàng)目模板初始化,集成IdentityServer4中間件;
集成IdentityServer4.AspNetIdentity用于管理User體系;
集成IdentityServer4.EntityFramework用于持久化OAuth2.0相關(guān)角色數(shù)據(jù);
集成NLog.Web.AspNetCore用于輸出日志;
集成System.Security.Cryptography.Csp使用RSA進(jìn)行Token簽名;
ApiResource(提供Api服務(wù),注冊(cè)到Passport中)所需組件
如果是Asp.Net Core WebApi項(xiàng)目:集成IdentityServer4.AccessTokenValidation(使用Authorize Policy機(jī)制即可);
如果是Asp.Net WebApi項(xiàng)目:集成IdentityServer3.AccessTokenValidation(中間件)+Thinktecture.IdentityModel.WebApi.ScopeAuthorization(提供ScopeAuthorize)
Client所需組件 IdentityModel(提供TokenClient)
注意事項(xiàng):
其一,ApiResource 無(wú)需配置ApiSecret(中間件會(huì)請(qǐng)求http://{passport-project-domain}/.well-known/openid-configuration/jwks獲取RSA公鑰驗(yàn)簽);
其二,Client端(調(diào)用方)必須設(shè)置密碼(clientId,clientSecret,請(qǐng)求所需scopes)
其三,配置RSA的時(shí)候,千萬(wàn)不能忘記設(shè)置KeyId,否則IdentityServer3.AccessTokenValidation獲取jwks時(shí)kid為null將拋異常。建議KeyId和Rsa的Base64字符串一起存配置文件。參考issue
搭建路線
IdentityServer4官方文檔:using-asp-net-core-identity
IdentityServer4官方文檔:using-entityframework-core-for-configuration-data
(可選)UI方面可臨時(shí)用quickstart-ui
(可選)如果是IdentityServer3,還有IdentityServer3.Admin和IdentityServer3.Admin.EntityFramework
Token簽名及驗(yàn)簽機(jī)制選擇:RSA。配置方法參考,再?gòu)?qiáng)調(diào)一遍千萬(wàn)不能忘記設(shè)置KeyId
ApiResource及Client的配置參考IdentityServer3和4各組件兼容性測(cè)試項(xiàng)目
Asp.Net WebApi(非.Net Core項(xiàng)目)對(duì)Scope進(jìn)行細(xì)粒度驗(yàn)證(落實(shí)到一個(gè)具體Api),使用ScopeAuthorize,安裝Nuget組件源碼參考
Install-Package Thinktecture.IdentityModel.WebApi.ScopeAuthorization
Asp.Net Core項(xiàng)目部署筆記
服務(wù)器安裝DotNetCore.1.0.4_1.1.1-WindowsHosting.exe
服務(wù)器管理員cmd執(zhí)行 net stop was /y && net start w3svc
新建站點(diǎn)
站點(diǎn)應(yīng)用程序池設(shè)為無(wú)托管代碼(IIS僅作為反向代理和方便站點(diǎn)發(fā)布)
啟用web部署
手動(dòng)vs發(fā)布,選iis,webdeploy,執(zhí)行連接字符串,指定遷移連接字符串
如果遇到IIS Aspnet Core Module啟動(dòng)dotnet進(jìn)程失敗,web.config啟用stdoutLogEnabled,并創(chuàng)建logs目錄,查看錯(cuò)誤信息
參考資源
IdentityServer4官方文檔
IdentityServer3和4各組件兼容性測(cè)試項(xiàng)目
IdentityServer4.Samples
validating-scopes-in-asp-net-4-and-5
ASP.NET Core實(shí)現(xiàn)OAuth2.0的ResourceOwnerPassword和ClientCredentials模式
NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj—vs2017)
Host on Windows with IIS
原文地址:http://personball.com/oauth2/2017/06/04/tips-of-identity-server-4-application
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺(tái)或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的IdentityServer4(OAuth2.0服务)折腾笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: EF通用数据层封装类(支持读写分离,一主
- 下一篇: DDD领域驱动之干货 (一)