1. 程式人生 > >IdentityServer 3.1.x 遷移到 4.x

IdentityServer 3.1.x 遷移到 4.x

### 一.前言 IdentityServer4 4.x已經正式釋出了,根據官方的 Release Note,3.1.x 到 4.x 的變更也是非常多,今天在將程式碼遷移到 4.x 遇到了一些問題在此記錄下來,我使用的 IdentityServer4 的各種資料持久化,比如 ClientStore,ResourceStore 等,都是完全自定義的,非 EntityFramework,如果你是使用的 EF 那麼官方提供了遷移指令碼,請自行查詢。 ## 二. Resource 相關變化 ApiResource 的 Scope 正式獨立出來為 `ApiScope` 物件,開發者可能是想讓使用者明白 ApiResource 和 Scope 的關係,而不是把它們混為一談,因為以前這一步都是 IdentityServer4 幫你做了,會自動以 ApiResource 的名稱為 Scope,所以可能存在認為它們兩個是相等的情況,實際則不然,Scope 是屬於 ApiResource 的一個屬性,可以包含多個 Scope。以原始碼來看: ### 1.ApiResource 的變化 3.1.x: ![image-20201014101034846](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000434521-725878177.png) 4.x: ![image-20201014101101701](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000434100-2078461172.png) ### 2.InMemory 執行方式的變化 對於大家在學習 IdentityServer4 時熟悉的 InMemory 來說,`AddInMemoryApiResources` 變為了 `AddInMemoryApiScopes` 也就是變了名字,但一定要明白這裡面實際的不同。 3.1.x: ![image-20201014102238481](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000433659-1131959164.png) 4.x: ![image-20201014102255505](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000433331-1736656086.png) ### 3.Resources 的變化 此類的構造方法變了,增加了 apiScopes 的引數,直接受影響的是 `IResourceStore `的實現,如果自定義了此介面的實現,那麼需要注意這個問題。 3.1.x: ![image-20201014102732082](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000432929-513700732.png) 4.x: ![image-20201014102818114](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000432538-1264052563.png) ### 4.IResourceStore 的變化 ![image-20201014104717646](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000432051-99679666.png) ## 三.PublicOrigin 屬性被刪除 IdentityServerOptions 物件中 `PublicOrigin` 屬性沒有了,此屬性涉及到在生產環境經過nginx反向代理等設施後 configuration endpoint 獲取到的地址的準確性,經過查詢官方的 Issues,目前有兩個解決方案: 1.在中介軟體中呼叫擴充套件方法 ````csharp app.Use(async (ctx, next) => { ctx.SetIdentityServerOrigin("https://foo.com"); await next(); }); ```` or ````csharp app.Use(async (ctx, next) => { ctx.Request.Scheme = "https"; ctx.Request.Host = new HostString("foo.com"); await next(); }); ```` 2.正確設定反向代理中的 `X-Forwarded-For`、`X-Forwarded-Proto`和`X-Forwarded-Host`,詳細設定請檢視:[Configure ASP.NET Core to work with proxy servers and load balancers](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-3.1&WT.mc_id=DT-MVP-5003133) 相關資料: - [how to change BaseUrl of the Identityserver #4857](https://github.com/IdentityServer/IdentityServer4/issues/4857#issuecomment-689719505) - [Where is PublicOrigin option now? #4535](https://github.com/IdentityServer/IdentityServer4/issues/4535) 關於此問題官方人員的原話:`It's gone. It was a hack - please use the forwarded headers approach in ASP.NET Core from now on.` 我個人更推薦**第二個方法** ## 四.IPersistedGrantStore 的變化 IPersistedGrantStore 介面的方法引數有變動: ![微信圖片_20201014235005](https://img2020.cnblogs.com/blog/668104/202010/668104-20201015000431443-189735882.png) ## 五.總結 以上就是我在 IdentityServer4 3.1.x 到 4.x 遷移所遇到的各個問題,所有問題都解決了並且成功執行。我是重度自定義介面實現,包括表都是自行設計的,非EF,自定義實現的介面如下:`IClientStore`、`IPersistedGrantStore`、`IProfileService`、`IResourceOwnerPasswordValidator`、`IResourceStore`。3.1.x 到 4.x 確實許多 break changes,但從遇到的幾個實際問題來說,開發人員在設計是已儘量相容了老版本,希望我所遇到的問題,能幫到大家。官方開了一個 issues 記錄遷移的問題,如果大家有難以解決的問題,不妨去提交:https://github.com/IdentityServer/IdentityServer4/issues/4592