public static void DispatchLoginRequest(string login, string password, string code, bool forceSms, Action <BackendResult <AutorizationData, ResultCode> > callback, CancellationToken?cancellationToken = null)
        {
            Dictionary <string, string> dictionary1 = new Dictionary <string, string>();

            dictionary1["grant_type"]    = "password";
            dictionary1["client_id"]     = VKConstants.ApplicationID;
            dictionary1["client_secret"] = VKConstants.ApplicationSecretKey;
            dictionary1["username"]      = login;
            dictionary1["password"]      = password;
            dictionary1["2fa_supported"] = "1";
            dictionary1["device_id"]     = AppGlobalStateManager.Current.GlobalState.DeviceId;

            if (!string.IsNullOrEmpty(code))
            {
                dictionary1["code"] = code;
            }
            else if (forceSms)
            {
                dictionary1["force_sms"] = "1";
            }
            dictionary1["scope"] = "notify, friends, photos, audio, video, docs, notes, pages, status, wall, groups, messages, notifications, stats, market";
            if (VKRequestsDispatcher.USE_HTTP)
            {
                dictionary1["scope"] = dictionary1["scope"] + ", nohttps";
            }
            VKRequestsDispatcher.DoDispatch <AutorizationData>(VKRequestsDispatcher.LoginUri, "", dictionary1, callback, new Func <string, AutorizationData>(JsonConvert.DeserializeObject <AutorizationData>), false, true, cancellationToken, null);
        }
 private static void ProcessValidationResponse <R>(ValidationUserResponse valResp, string baseUrl, string methodName, Dictionary <string, string> parameters, Action <BackendResult <R, ResultCode> > callback, Func <string, R> customDeserializationFunc = null, bool lowPriority = false, bool pageDataRequest = true, CancellationToken?cancellationToken = null)
 {
     if (valResp.IsSucceeded)
     {
         int num = !string.IsNullOrEmpty(AppGlobalStateManager.Current.GlobalState.AccessToken) ? 1 : 0;
         if (!string.IsNullOrEmpty(valResp.access_token))
         {
             VKRequestsDispatcher._autorizationData.access_token   = valResp.access_token;
             AppGlobalStateManager.Current.GlobalState.AccessToken = valResp.access_token;
         }
         if (num != 0)
         {
             VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null);
         }
         else
         {
             string str = string.Format("{{\"access_token\":\"{0}\",\"user_id\":{1} }} ", valResp.access_token, valResp.user_id);
             R      r   = customDeserializationFunc(str);
             VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.Succeeded, r))), cancellationToken);
         }
     }
     else
     {
         VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.ValidationCancelledOrFailed))), cancellationToken);
     }
 }
 private static void DoDispatch <R>(string baseUrl, string methodName, Dictionary <string, string> parameters, Action <BackendResult <R, ResultCode> > callback, Func <string, R> customDeserializationFunc = null, bool lowPriority = false, bool pageDataRequest = true, CancellationToken?cancellationToken = null, Action confirmationRequiredHandler = null)
 {
     parameters["v"] = VKConstants.API_VERSION;
     if (!string.IsNullOrEmpty(VKRequestsDispatcher._autorizationData != null ? VKRequestsDispatcher._autorizationData.access_token : null))
     {
         parameters["access_token"] = VKRequestsDispatcher._autorizationData.access_token;
     }
     VKRequestsDispatcher.AddLangParameter(parameters);
     if (!string.IsNullOrEmpty(VKRequestsDispatcher._autorizationData != null ? VKRequestsDispatcher._autorizationData.secret : null))
     {
         if (parameters.ContainsKey("sig"))
         {
             parameters.Remove("sig");
         }
         string str1 = JsonWebRequest.ConvertDictionaryToQueryString(parameters, false);
         if (str1 != string.Empty)
         {
             str1 = "?" + str1;
         }
         string str2 = VKRequestsDispatcher.HashString("/method/" + methodName + str1 + VKRequestsDispatcher._autorizationData.secret);
         parameters["sig"] = str2.ToLower();
     }
     JsonWebRequest.SendHTTPRequestAsync(!VKRequestsDispatcher.USE_HTTP ? "https://" + baseUrl : "http://" + baseUrl, parameters, (Action <JsonResponseData>)(jsonResp =>
     {
         BackendResult <R, ResultCode> backendResult = new BackendResult <R, ResultCode>(ResultCode.CommunicationFailed);
         if (jsonResp.IsSucceeded)
         {
             VKRequestsDispatcher.ResultData resultFromJson = VKRequestsDispatcher.GetResultFromJson(JObject.Parse(jsonResp.JsonString));
             backendResult.ResultCode = resultFromJson.ResultCode;
             backendResult.Error      = resultFromJson.error;
             if (backendResult.ResultCode == ResultCode.UserAuthorizationFailed)
             {
                 if (!string.IsNullOrEmpty(VKRequestsDispatcher._autorizationData != null ? VKRequestsDispatcher._autorizationData.access_token : null))
                 {
                     Logger.Instance.Error("RECEIVED AUTHORIZATION FAILURE!!! JSON STR = " + jsonResp.JsonString ?? "");
                     LogoutRequestHandler.InvokeLogoutRequest();
                 }
             }
             if (backendResult.ResultCode == ResultCode.CaptchaRequired)
             {
                 CaptchaUserRequestHandler.InvokeCaptchaRequest(new CaptchaUserRequest()
                 {
                     CaptchaSid = resultFromJson.captcha_sid,
                     Url        = resultFromJson.captcha_img
                 }, (Action <CaptchaUserResponse>)(captchaResp =>
                 {
                     if (!captchaResp.IsCancelled)
                     {
                         Dictionary <string, string> parameters1 = parameters;
                         parameters1["captcha_sid"] = captchaResp.Request.CaptchaSid;
                         parameters1["captcha_key"] = captchaResp.EnteredString;
                         VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters1, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null);
                     }
                     else
                     {
                         VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.CaptchaControlCancelled))), cancellationToken);
                     }
                 }));
             }
             else if (backendResult.ResultCode == ResultCode.ValidationRequired)
             {
                 if (resultFromJson.validation_type == "2fa_app" || resultFromJson.validation_type == "2fa_sms")
                 {
                     if (parameters.ContainsKey("force_sms") || parameters.ContainsKey("code"))
                     {
                         R r = customDeserializationFunc != null ? customDeserializationFunc("") : default(R);
                         VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.Succeeded, r))), cancellationToken);
                     }
                     else
                     {
                         CaptchaUserRequestHandler.InvokeValidation2FARequest(new Validation2FAUserRequest()
                         {
                             username       = parameters.ContainsKey("username") ? parameters["username"] : "",
                             password       = parameters.ContainsKey("password") ? parameters["password"] : "",
                             phoneMask      = resultFromJson.phone_mask,
                             validationType = resultFromJson.validation_type,
                             validationSid  = resultFromJson.validation_sid
                         }, (Action <ValidationUserResponse>)(valResp => VKRequestsDispatcher.ProcessValidationResponse <R>(valResp, baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken)));
                     }
                 }
                 else
                 {
                     CaptchaUserRequestHandler.InvokeValidationRequest(new ValidationUserRequest()
                     {
                         ValidationUri = resultFromJson.redirect_uri
                     }, (Action <ValidationUserResponse>)(valResp => VKRequestsDispatcher.ProcessValidationResponse <R>(valResp, baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken)));
                 }
             }
             else if (backendResult.ResultCode == ResultCode.ConfirmationRequired)
             {
                 if (!VKRequestsDispatcher.GetIsResponseCancelled(cancellationToken))
                 {
                     Action action = confirmationRequiredHandler;
                     if (action != null)
                     {
                         action();
                     }
                     IBackendConfirmationHandler confirmationHandler = ServiceLocator.Resolve <IBackendConfirmationHandler>();
                     if (confirmationHandler != null)
                     {
                         confirmationHandler.Confirm(resultFromJson.confirmation_text, (Action <bool>)(confirmed =>
                         {
                             if (confirmed)
                             {
                                 parameters["confirm"] = "1";
                                 VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null);
                             }
                             else
                             {
                                 VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.ConfirmationCancelled))), cancellationToken);
                             }
                         }));
                     }
                 }
             }
             else if (backendResult.ResultCode == ResultCode.NotEnoughMoney)
             {
                 if (!VKRequestsDispatcher.GetIsResponseCancelled(cancellationToken))
                 {
                     IBackendNotEnoughMoneyHandler enoughMoneyHandler = ServiceLocator.Resolve <IBackendNotEnoughMoneyHandler>();
                     Action action1 = (() => VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null));
                     Action action2 = (Action)(() => VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.BalanceRefillCancelled))), cancellationToken));
                     if (enoughMoneyHandler != null)
                     {
                         Action refilledCallback  = action1;
                         Action cancelledCallback = action2;
                         enoughMoneyHandler.RequestBalanceRefill(refilledCallback, cancelledCallback);
                     }
                 }
             }
             else if (backendResult.ResultCode == ResultCode.Succeeded)
             {
                 try
                 {
                     List <ExecuteError> executeErrorList = null;
                     R r;
                     if (customDeserializationFunc != null)
                     {
                         r = customDeserializationFunc(jsonResp.JsonString);
                     }
                     else if (typeof(R) == typeof(VKClient.Common.Backend.DataObjects.ResponseWithId))
                     {
                         r = JsonConvert.DeserializeObject <R>(jsonResp.JsonString);
                     }
                     else
                     {
                         VKRequestsDispatcher.GenericRoot <R> genericRoot = JsonConvert.DeserializeObject <VKRequestsDispatcher.GenericRoot <R> >(jsonResp.JsonString);
                         r = genericRoot.response;
                         executeErrorList = genericRoot.execute_errors;
                     }
                     backendResult.ResultData    = r;
                     backendResult.ExecuteErrors = executeErrorList;
                 }
                 catch (Exception ex)
                 {
                     Logger.Instance.Error("Error during deserialization", ex);
                     backendResult.ResultCode = ResultCode.DeserializationError;
                 }
             }
         }
         if (backendResult.ResultCode == ResultCode.CaptchaRequired || backendResult.ResultCode == ResultCode.ValidationRequired || (backendResult.ResultCode == ResultCode.ConfirmationRequired || backendResult.ResultCode == ResultCode.NotEnoughMoney))
         {
             return;
         }
         VKRequestsDispatcher.InvokeCallback((Action)(() =>
         {
             Action <BackendResult <R, ResultCode> > action = callback;
             if (action == null)
             {
                 return;
             }
             BackendResult <R, ResultCode> backendResult1 = backendResult;
             action(backendResult1);
         }), cancellationToken);
     }), true, lowPriority, pageDataRequest);
 }
 //////////
 public static void DispatchRequestToVK <R>(string methodName, Dictionary <string, string> parameters, Action <BackendResult <R, ResultCode> > callback, Func <string, R> customDeserializationFunc = null, bool lowPriority = false, bool pageDataRequest = true, CancellationToken?cancellationToken = null, Action confirmationRequiredHandler = null)
 {
     VKRequestsDispatcher.DoDispatch <R>(string.Format(VKRequestsDispatcher.RequestUriFrm, methodName), methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, confirmationRequiredHandler);
 }