/// <summary> /// Callback method called after request.BeginGetResponse completes. /// </summary> /// <param name="asyncResult"> /// An asyncResult object. /// </param> private void ResponseCallback(IAsyncResult asyncResult) { AdmTokenServiceRequestState stateObject = (AdmTokenServiceRequestState)asyncResult.AsyncState; try { HttpWebRequest state = stateObject.Request; using (HttpWebResponse response = (HttpWebResponse)state.EndGetResponse(asyncResult)) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken)); this.accessToken = (AdmAccessToken)serializer.ReadObject(response.GetResponseStream()); this.accessToken.ExpriesAtUtc = DateTime.UtcNow.AddMinutes(TokenExpiredInMinutes); this.OnRetriveAdmAccessTokenComplete(this.accessToken, null, stateObject.Callback); } } catch (Exception ex) { this.OnRetriveAdmAccessTokenComplete(null, ex, stateObject.Callback); } }
/// <summary> /// The callback handler of GetAdmAccessToken event of AdmTokenService. /// </summary> /// <param name="accessToken">The token instance.</param> /// <param name="ex">Coressponding exception if failed to get the access token.</param> /// <param name="callback">callback from event</param> private void TokenService_GetAdmAccessTokenCompleteEvent(AdmAccessToken accessToken, Exception ex, RetriveAccessTokenComplete callback) { if (ex == null) { string token = string.Empty; if (!string.IsNullOrEmpty(this.RegistrationId) && !string.IsNullOrEmpty(this.SecretKey)) { token = string.Format("BEARER {0} {1} {2}", accessToken.AccessToken, this.SecretKey, this.RegistrationId); } else { token = string.Format("BEARER {0}", accessToken.AccessToken); } this.OnRetriveAccessTokenComplete(token, ex, callback); } else { this.OnRetriveAccessTokenComplete(string.Empty, ex, callback); } }
/// <summary> /// Help method to fire the RetriveAccessTokenCompleteEvent event. /// </summary> /// <param name="accessToken">The accesss token string</param> /// <param name="ex">Coressponding exception if failed to get the access token.</param> /// <param name="callback">callback from event</param> protected virtual void OnRetriveAdmAccessTokenComplete(AdmAccessToken accessToken, Exception ex, RetriveAdmAccessTokenComplete callback) { callback(accessToken, ex); }