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