/// <summary> /// 获取解密后token /// <param name="token">token</param> /// <returns>解密后token<</returns> public static string GetToken(string token) { if (string.IsNullOrEmpty(token)) { return(token); } return(ScryptHelper.DecryptDES(Microsoft.JScript.GlobalObject.decodeURIComponent(token))); }
/// <summary> /// Callback /// </summary> public ActionResult Callback(string flag, string language) { try { _log.Debug($"Callback Start"); OAuth2Result auth2Result = null; bool oauth2 = OAuthHelper.TryGetOAuth2Result(flag, out auth2Result); var code = Request.QueryString["code"]; _log.Debug($"Callback Code: '{code}'"); if (!oauth2 || string.IsNullOrEmpty(code)) { _log.Debug($"Callback Invalid Param."); return(Redirect($"/Login/Error?language={language}")); } else { var redirectUrl = $"{Request.Url.Scheme}://{Request.Url.Authority}/Login/Callback?flag={flag}&language={language}"; var basic = Convert.ToBase64String(Encoding.Default.GetBytes($"{auth2Result.ClientId}:{auth2Result.ClientSecret}")); string resStr = string.Empty; var handler = new WebRequestHandler(); handler.ServerCertificateValidationCallback = delegate { return(true); }; using (var httpClient = new HttpClient(handler)) { httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + basic); var json = $"grant_type=authorization_code&code={code}&redirect_uri={CommonHelper.EncodeUrl(redirectUrl)}"; var content = new StringContent(json, Encoding.UTF8, "application/x-www-form-urlencoded"); var res = httpClient.PostAsync($"{Constant.OAuth2Url}/oauth2/token", content).Result; resStr = res.Content.ReadAsStringAsync().Result; _log.Debug($"Callback oauth2 token: '{resStr}'"); } var obj = JsonConvert.DeserializeObject <dynamic>(resStr); auth2Result.AccessToken = ScryptHelper.EncryptDES($"Bearer {obj["access_token"].Value}"); auth2Result.ExpireIn = DateTime.Now.AddSeconds(obj["expires_in"].Value); auth2Result.IdToken = obj["id_token"].Value; auth2Result.RefreshToken = ScryptHelper.EncryptDES(obj["refresh_token"].Value); OAuthHelper.SetOAuth2Result(flag, auth2Result); _log.Debug($"Callback End"); return(Redirect($"/Login/Success?language={language}")); } } catch (Exception ex) { _log.Debug($"Callback Exception: {ex.Message}"); return(Redirect($"/Login/Error?language={language}")); } }
public JsonResult RefreshToken(OAuth2Result model) { try { // 删除日志 HangfireHelper.StartHanfireWork(); var clientId = model.ClientId; var clientSecret = model.ClientSecret; var refreshToken = ScryptHelper.DecryptDES(model.RefreshToken); var basic = Convert.ToBase64String(Encoding.Default.GetBytes($"{clientId}:{clientSecret}")); string resStr = string.Empty; var handler = new WebRequestHandler(); handler.ServerCertificateValidationCallback = delegate { return(true); }; using (var httpClient = new HttpClient(handler)) { httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + basic); var json = $"grant_type=refresh_token&refresh_token={refreshToken}"; var content = new StringContent(json, Encoding.UTF8, "application/x-www-form-urlencoded"); var res = httpClient.PostAsync($"{Constant.OAuth2Url}/oauth2/token", content).Result; resStr = res.Content.ReadAsStringAsync().Result; _log.Debug($"RefreshToken oauth2 token: '{resStr}'"); } var obj = JsonConvert.DeserializeObject <dynamic>(resStr); OAuth2Result result = new OAuth2Result() { AccessToken = ScryptHelper.EncryptDES($"Bearer {obj["access_token"].Value}"), ExpireIn = DateTime.Now.AddSeconds(obj["expires_in"].Value), IdToken = obj["id_token"].Value, RefreshToken = ScryptHelper.EncryptDES(obj["refresh_token"].Value), ClientId = model.ClientId, ClientSecret = model.ClientSecret }; return(Json(new { success = true, token = JsonConvert.SerializeObject(result) })); } catch (Exception ex) { _log.Debug($"RefreshToken Parameter: {JsonConvert.SerializeObject(model)}, Exception: {ex.Message}"); return(Json(new { success = false })); } }