public async Task <authModel> UpdateToken(string tokenType) //function that will get the refresh token and the access token { FormUrlEncodedContent request; if (tokenType.Equals("new")) { request = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", Username), new KeyValuePair <string, string>("password", Password), new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("client_id", ClientId) }); } else { request = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "refresh_token"), new KeyValuePair <string, string>("client_id", "mtb"), new KeyValuePair <string, string>("refresh_token", refreshtoken) }); } HttpResponseMessage response = await client.PostAsync("https://api.staging.payfone.com/token", request); tokenTime = DateTime.Now; response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStringAsync(); authModel model = JsonConvert.DeserializeObject <authModel>(responseBody); return(model); }
// Ctrl + k then Ctrl + c to comment all // ctrl + k then ctrl + u to uncomment all public async Task <string> ProcessToken() //function for actual getting the actual token { var temp = accesstoken; if (String.IsNullOrEmpty(accesstoken)) //if no accesstoken then get the the tokens { authModel model = await UpdateToken("new"); accesstoken = model.access_token; expires_in = TimeSpan.FromSeconds(Convert.ToInt64(model.expires_in) * 0.9); refresh_expires_in = TimeSpan.FromSeconds(Convert.ToInt64(model.refresh_expires_in) * 0.9); refreshtoken = model.refresh_token; return(temp); } else if (ifTimeExpired()) //if time expires { authModel model = await UpdateToken("refresh"); accesstoken = model.access_token; expires_in = TimeSpan.FromSeconds(Convert.ToInt64(model.expires_in) * 0.9); refresh_expires_in = TimeSpan.FromSeconds(Convert.ToInt64(model.refresh_expires_in) * 0.9); refreshtoken = model.refresh_token; return("Time"); } else { return("Nothing"); } }
public string authlogin() { //string userName = "******"; //string password = "******"; try { string postData = Request.Content.ReadAsStringAsync().Result; var authData = JsonConvert.DeserializeObject <authModel>(postData); #region testData /* test data */ authData = new authModel(); authData.userName = "******"; authData.password = "******"; /* test data */ #endregion var data = "grant_type=password" + "&username="******"&password="******"&client_id=" + client_id; byte[] bytes = Encoding.UTF8.GetBytes(data); WebClient webClient = new WebClient(); webClient.Headers.Clear(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); var rev = Encoding.UTF8.GetString(webClient.UploadData(serviceBase, bytes)); return(rev);/* * var dataObj = JsonConvert.DeserializeObject<objTokenAfterLogin>(rev); * dataTempAuth = dataObj; * return dataTempAuth;*/ } catch (Exception e) { throw e; } }
public async Task <string> authanythingpost() { try { //string postData = Request.Content.ReadAsStringAsync().Result; //authModel authData = JsonConvert.DeserializeObject<authModel>(postData); #region => dummy data var authData = new authModel(); authData.userName = "******"; authData.password = "******"; dataTempAuth.access_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"; dataTempAuth.refresh_token = "d9d0ca308de64e"; dataTempAuth.token_type = "bearer "; #endregion anythingData andt = new anythingData { aaa1 = "ax01", bbb2 = "ax02", ccc3 = "ax03" }; string data = JsonConvert.SerializeObject(authData); byte[] bytes = Encoding.UTF8.GetBytes(data); WebClient webClient = new WebClient(); webClient.Headers.Clear(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); webClient.Headers.Add("Authorization", dataTempAuth.token_type + dataTempAuth.access_token); string apiname = "test/anythingpost"; resBase = "http://localhost:3000/"; string rev = Encoding.UTF8.GetString(webClient.UploadData(resBase + apiname, bytes)); anythingData dataObj = JsonConvert.DeserializeObject <anythingData>(rev); objTokenAfterLogin retToken = new objTokenAfterLogin(); retToken.refresh_token = rev; return(rev); //return retToken; } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { var response = ex.Response as HttpWebResponse; if (response != null) { if ((int)response.StatusCode == 401) { if (dataTempAuth != null) { var usingBuilkAuthToken = false; if (response.ResponseUri.ToString().Split('/')[2] == resBase.Split('/')[2].ToString()) { usingBuilkAuthToken = true; } if (usingBuilkAuthToken) { var taskUpload = RefreshToken(); var waitForRefreshTokens = await taskUpload; return(waitForRefreshTokens); } } } } else { // no http status code available } } else { // no http status code available } throw ex; } }