//結合EFCore生成IdentityServer4數據庫 // 項目工程文件最后添加 <ItemGroup><DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /></ItemGroup> //dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb //dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb //黎又銘 Add 2017-11-28 添加IdentityServer4對EFCore數據庫的支持 //但是這里需要初始化數據 默認生成的數據庫中是沒有配置數據 const string connectionString = @"Data Source=192.168.0.42;database=A.IdentityServer4;trusted_connection=yes;"; var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; services.AddIdentityServer(idroptions => { //設置將在發現文檔中顯示的頒發者名稱和已發布的JWT令牌。建議不要設置此屬性,該屬性從客戶端使用的主機名中推斷頒發者名稱 idroptions.IssuerUri = ""; //設置認證 idroptions.Authentication = new IdentityServer4.Configuration.AuthenticationOptions { CheckSessionCookieName = "idr.Cookies",//用於檢查會話端點的cookie的名稱 CookieLifetime = new TimeSpan(1, 0, 0),//身份驗證Cookie生存期(僅在使用IdentityServer提供的Cookie處理程序時有效) CookieSlidingExpiration = true,//指定cookie是否應該滑動(僅在使用IdentityServer提供的cookie處理程序時有效) RequireAuthenticatedUserForSignOutMessage = true //指示是否必須對用戶進行身份驗證才能接受參數以結束會話端點。默認為false }; //活動事件 允許配置是否應該將哪些事件提交給注冊的事件接收器 idroptions.Events = new IdentityServer4.Configuration.EventsOptions { RaiseErrorEvents = true, RaiseFailureEvents = true, RaiseSuccessEvents = true, RaiseInformationEvents = true }; //允許設置各種協議參數(如客戶端ID,范圍,重定向URI等)的長度限制 idroptions.InputLengthRestrictions = new IdentityServer4.Configuration.InputLengthRestrictions { //可以看出下面很多參數都是對長度的限制 AcrValues = 100, AuthorizationCode = 100, ClientId = 100, /* .. .. .. */ ClientSecret = 1000 }; //用戶交互頁面定向設置處理 idroptions.UserInteraction = new IdentityServer4.Configuration.UserInteractionOptions { LoginUrl = "http://192.168.0.42:5001/Login",//【必備】登錄地址 LogoutUrl = "http://192.168.0.42:5001/Logout",//【必備】退出地址 ConsentUrl = "http://192.168.0.42:5001/Consent",//【必備】允許授權同意頁面地址 ErrorUrl = "http://192.168.0.42:5001/Error", //【必備】錯誤頁面地址 LoginReturnUrlParameter = "returnUrl",//【必備】設置傳遞給登錄頁面的返回URL參數的名稱。默認為returnUrl LogoutIdParameter = "logoutId", //【必備】設置傳遞給注銷頁面的注銷消息ID參數的名稱。缺省為logoutId ConsentReturnUrlParameter = "returnUrl", //【必備】設置傳遞給同意頁面的返回URL參數的名稱。默認為returnUrl ErrorIdParameter = "errorId", //【必備】設置傳遞給錯誤頁面的錯誤消息ID參數的名稱。缺省為errorId CustomRedirectReturnUrlParameter = "returnUrl", //【必備】設置從授權端點傳遞給自定義重定向的返回URL參數的名稱。默認為returnUrl CookieMessageThreshold = 5 //【必備】由於瀏覽器對Cookie的大小有限制,設置Cookies數量的限制,有效的保證了瀏覽器打開多個選項卡,一旦超出了Cookies限制就會清除以前的Cookies值 }; //緩存參數處理 緩存起來提高了效率 不用每次從數據庫查詢 idroptions.Caching = new IdentityServer4.Configuration.CachingOptions { ClientStoreExpiration = new TimeSpan(1, 0, 0),//設置Client客戶端存儲加載的客戶端配置的數據緩存的有效時間 ResourceStoreExpiration = new TimeSpan(1, 0, 0),// 設置從資源存儲加載的身份和API資源配置的緩存持續時間 CorsExpiration = new TimeSpan(1, 0, 0) //設置從資源存儲的跨域請求數據的緩存時間 }; //IdentityServer支持一些端點的CORS。底層CORS實現是從ASP.NET Core提供的,因此它會自動注冊在依賴注入系統中 idroptions.Cors = new IdentityServer4.Configuration.CorsOptions { CorsPaths = { ""}, //支持CORS的IdentityServer中的端點。默認為發現,用戶信息,令牌和撤銷終結點 CorsPolicyName = "default", //【必備】將CORS請求評估為IdentityServer的CORS策略的名稱(默認為"IdentityServer4")。處理這個問題的策略提供者是ICorsPolicyService在依賴注入系統中注冊的。如果您想定制允許連接的一組CORS原點,則建議您提供一個自定義的實現ICorsPolicyService PreflightCacheDuration = new TimeSpan(1, 0, 0)//可為空的<TimeSpan>,指示要在預檢Access-Control-Max-Age響應標題中使用的值。默認為空,表示在響應中沒有設置緩存頭 }; }).AddConfigurationStore(options => { options.ConfigureDbContext = builder => { builder.UseSqlServer(connectionString, builderoptions => { builderoptions.MigrationsAssembly(migrationsAssembly); }); }; }) .AddOperationalStore(options => { options.ConfigureDbContext = builder => { builder.UseSqlServer(connectionString, builderoptions => { builderoptions.MigrationsAssembly(migrationsAssembly); }); }; options.EnableTokenCleanup = true; //允許對Token的清理 options.TokenCleanupInterval = 1800; //清理周期時間Secends }) ;
之前一直用的是IdentityServer3 ,現在逐步開始學習IdentityServer4來處理,先從配置開始把:
訪問下配置頁面看下:
好像沒問題