public async Task <IActionResult> GetClaims() { var model = new GetClaimViewModel(); var identity = HttpContext.User.Identity as ClaimsIdentity; //從Cookie取出token var Get_access_token = new ResultModel(); Get_access_token = _cookieHelper.Get("access_token"); if (Get_access_token.IsSuccess) { TempData["access_token"] = Get_access_token.Data.ToString(); } var Get_refresh_token = new ResultModel(); Get_refresh_token = _cookieHelper.Get("refresh_token"); if (Get_refresh_token.IsSuccess) { TempData["refresh_token"] = Get_refresh_token.Data.ToString(); } if (identity != null) { model.claims = identity.Claims; } return(View(model)); }
public T Get <T>(string name) { try { var cookieString = _encodeCookie ? _cookieHelper.GetProtect(name) : _cookieHelper.Get(name); if (string.IsNullOrEmpty(cookieString)) { return(default(T)); } var obj = JsonConvert.DeserializeObject <T>(cookieString); return(obj); } catch { return(default(T)); } }
public async Task <ResultModel> CallApi(List <IFormFile> files, string url, string folder) { var result = new ResultModel(); var Get_access_token = new ResultModel(); Get_access_token = _cookieHelper.Get("access_token"); if (Get_access_token.IsSuccess) { using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Access-Control-Allow-Origin", _config["api"]); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Get_access_token.Data.ToString()); using (var form = new MultipartFormDataContent()) { foreach (var i in files) { var fs = i.OpenReadStream(); var streamContent = new StreamContent(fs); var fileContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync()); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); form.Add(fileContent, "files", i.FileName); } form.Add(new StringContent(folder, Encoding.UTF8), "folder"); try { HttpResponseMessage response = await httpClient.PostAsync(url, form); string strResult = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <ResultModel>(strResult.ToString()); } catch (Exception e) { var i = e; } } } } else { result.IsSuccess = false; result.Message = "請重新登入"; } return(result); }
public async Task <ResultModel> CallAPI(string data, Uri url, string method) { HttpWebRequest request; request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.ContentType = "application/json"; if (method == "POST" || method == "PUT") { request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.ContentType = "application/json"; //要發送的字串轉為byte[] byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data); using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } } var cookieHelper = new CookieHelper(_httpContextAccessor, _config); var Get_access_token = new ResultModel(); Get_access_token = _cookieHelper.Get("access_token"); if (Get_access_token.IsSuccess) { request.Headers.Add("Authorization", "Bearer " + Get_access_token.Data); } ResultModel result = new ResultModel(); string responseStr = "test"; //發出Request //接收回應 try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseStr = reader.ReadToEnd(); result = JsonConvert.DeserializeObject <ResultModel>(responseStr); } } } catch (WebException e) { result.IsSuccess = false; using (HttpWebResponse response = e.Response as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.Unauthorized) { result.Message = "401"; var RefreshTokenResult = new ResultModel(); RefreshTokenResult = await RefreshMyToken(); if (RefreshTokenResult.IsSuccess) { result.IsSuccess = false; result.Message = "Token已更新,請重新整理頁面"; //result = await CallAPI(data, url, method);//會抓到舊cookie 有bug } else { result = RefreshTokenResult; //if(result.Data!=null) if (result.Message.ToString().Contains("IDX10000")) { result.Message = "Token過期,請重新登入,或是勾選保持登入"; } } } else { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseStr = reader.ReadToEnd(); result.IsSuccess = false; result.Message = responseStr; } } } } return(result); }