public async Task <TokenResponse> LoginToIds4Async(HttpContext httpContext, string usename, string password, string refreshToken = null) { var identityserver4url = AppConfig.GetDicstring("Identityserver4.url"); var ClientId = AppConfig.GetDicstring("idsr4_ClientId"); var ClientSecret = AppConfig.GetDicstring("idsr4_ClientSecret"); var clientmodel = new OauthClientModel() { IdentityUrl = identityserver4url, ClientId = ClientId, ClientSecret = ClientSecret, UserName = usename, Password = password, RefreshToken = refreshToken }; return(await LoginToIds4Async(httpContext, clientmodel)); }
public async Task <TokenResponse> LoginToIds4Async(HttpContext httpContext, OauthClientModel clientmodel) { if (!string.IsNullOrEmpty(clientmodel.AccessToken) && clientmodel.ExpiresTime > DateTime.Now) { return(null); } var disco = await Client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest() { Address = clientmodel.IdentityUrl, Policy = { RequireHttps = false } }); var refreshToken = clientmodel.RefreshToken; //获取用户角色 //var userres = RequestUserInfo(disco.UserInfoEndpoint, Client, clientmodel.AccessToken); //Token作废 TokenResponse RequesttokenResponse = null; if (refreshToken != null && refreshToken.Length > 20) { RequesttokenResponse = await Client.RequestRefreshTokenAsync(new RefreshTokenRequest() { Address = disco.TokenEndpoint, ClientId = clientmodel.ClientSecret, ClientSecret = clientmodel.ClientSecret, Scope = clientmodel.Scope, GrantType = OpenIdConnectGrantTypes.RefreshToken, RefreshToken = refreshToken, }); if (RequesttokenResponse.IsError) { RequesttokenResponse = await LoginGetAccesstoken(Client, clientmodel.ClientId, clientmodel.ClientSecret, clientmodel.UserName, clientmodel.Password, disco.TokenEndpoint); } } else { RequesttokenResponse = await LoginGetAccesstoken(Client, clientmodel.ClientId, clientmodel.ClientSecret, clientmodel.UserName, clientmodel.Password, disco.TokenEndpoint); } var expiresAt = DateTime.Now + TimeSpan.FromSeconds(RequesttokenResponse.ExpiresIn); var tokens = new[] { new AuthenticationToken { Name = OpenIdConnectParameterNames.IdToken, Value = RequesttokenResponse.IdentityToken }, new AuthenticationToken { Name = OpenIdConnectParameterNames.AccessToken, Value = RequesttokenResponse.AccessToken }, new AuthenticationToken { Name = OpenIdConnectParameterNames.RefreshToken, Value = RequesttokenResponse.RefreshToken }, new AuthenticationToken { Name = "expires_at", Value = expiresAt.ToString("o", CultureInfo.InvariantCulture) } }; // 获取身份认证的结果,包含当前的pricipal和 properties var currentAuthenticateResult = await httpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme); // 把新的tokens存起来 currentAuthenticateResult.Properties.StoreTokens(tokens); // 登陆 await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, currentAuthenticateResult.Principal, currentAuthenticateResult.Properties); clientmodel.AccessToken = RequesttokenResponse.AccessToken; clientmodel.RefreshToken = RequesttokenResponse.RefreshToken; clientmodel.ExpiresTime = expiresAt; return(RequesttokenResponse); }