Idsv4是不關心客戶端是誰的,我有一些想法,不知道是不是你需要的。
首先在Core
的IdentityServer4
上自定義登錄地址。
services.AddIdentityServer(options =>
{ // 忽略
options.UserInteraction.LoginUrl = Configuration["ApplicationDTO:LoginUrl"]; // 假設是/users/signIn
options.UserInteraction.LogoutUrl = Configuration["ApplicationDTO:LogoutUrl"]; /
})
新建一個UsersControllers
.添加signIn
的Action
.
[HttpGet]
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)] public async Task<IActionResult> SignIn(string returnUrl) { //通過驗證后即清除cookies
await HttpContext.SignOutAsync("Cookies"); #region Issued Cookie
List<Claim> source = new List<Claim>()
{ new Claim("sub",new Guid().ToString()), new Claim("name",User.Identity.Name), new Claim("idp", "xxxxx"), new Claim("role","Custom"), new Claim("auth_time", DateTimeOffset.Now.ToEpochTime().ToString(),"http://www.w3.org/2001/XMLSchema#integer")
};
source.Add(new Claim("amr", "authorization_code")); var identity = new ClaimsIdentity(source.Distinct<Claim>((IEqualityComparer<Claim>)new ClaimComparer()), "IdentityServer4", "name", "role"); var claimsPrincipal = new ClaimsPrincipal(identity); await HttpContext.SignInAsync(IdentityServerConstants.DefaultCookieAuthenticationScheme, claimsPrincipal, new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.Now.Add(TimeSpan.FromMinutes(43200))
}); #endregion
return Redirect(returnUrl);
}
Authorize
需要您在startup
自己定義登錄地址。
那么流程就是如下這樣的
1.在瀏覽器訪問idsv4
服務端https://Coreidsv4/connect/authorize?......
,會跳轉(zhuǎn)到/users/login
通過Authorize
驗證用戶是否登錄,如果未登錄就通過Authorization配置的登錄地址去登錄。登錄成功重定向回來??梢栽?code>users/login中填寫自己要信息。繼續(xù)下去,通過url
獲取授權碼 code
。然后再去拿code
去換取token
.注意參數(shù)填寫正確。