Exemple #1
0
        /// <summary>
        /// Авторизация пользователя по токену
        /// </summary>
        /// <param name="token">Токен</param>
        /// <param name="errorResponse"></param>
        /// <returns>Объект с результатом аутентификации </returns>
        public AuthLoginResponse AuthLoginByToken(string token, string uid)
        {
            var authLoginResponse = new AuthLoginResponse();

            authLoginResponse.uid = uid;
            authLoginResponse.auth_token = token;

            // формирование запроса
            Uri uri = new Uri();
            uri.Address = SystemConfiguration.ServerConnectionToLogin;
            uri.Method = "/auth?login=auto&site=1";

            var request = HttpUtility.PrepareHttpWebRequest(uri.GetUri());

            try
            {
                request.Headers["Cookie"] = "remixpassword="******"AuthLoginByToken requested: Address: {0} Header: {1}", request.Address, request.Headers), null));
                }

                // прием ответа

                using (WebResponse newHttpWebResponse = request.GetResponse())
                {
                    WebHeaderCollection whc = newHttpWebResponse.Headers;

                    if (whc != null)
                    {
                        try
                        {
                            string loc = whc["Location"];

                            string tmpStr = loc.Substring(loc.IndexOf("sid=") + "sid=".Length);

                            if (tmpStr == "-1")
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else if (tmpStr == "-2")
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else if (tmpStr == "-3")
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else if (tmpStr == "-4")
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else
                            {
                                authLoginResponse.session_key = tmpStr;
                            }
                        }
                        catch (ArgumentException)
                        {
                            authLoginResponse.session_key = string.Empty;
                        }

                        return authLoginResponse;
                    }
                }
            }
            catch (ObjectDisposedException ex)
            {
                DebugHelper.WriteLogEntry(ex, "AuthLoginByToken ObjectDisposedException");

                return null;
            }
            finally
            {
                request.Abort();
            }

            return null;
        }
Exemple #2
0
        /// <summary>
        /// Авторизация пользователя по логину и паролю
        /// </summary>
        /// <param name="login">Логин пользователя </param>
        /// <param name="pass">Пароль </param>
        /// <param name="isRemember">Флаг, указывающий на необходимость получения токена </param>
        /// <param name="errorResponse"></param>
        /// <returns>Объект с результатом аутентификации </returns>
        public AuthLoginResponse AuthLogin(string login, string pass, bool isRemember)
        {
            var authLoginResponse = new AuthLoginResponse();

            // формирование запроса
            var uri = new Uri
                          {
                              Address = SystemConfiguration.ServerConnectionToLogin,
                              Method = "/auth?login=force&"
                          };

            uri.Parameters.Add("site=1");
            uri.Parameters.Add("email=" + login);
            uri.Parameters.Add("pass="******"AuthLogin requested: Address: {0} Header: {1}", request.Address, request.Headers), null));
                    }

                    // прием ответа
                    WebHeaderCollection whc = newHttpWebResponse.Headers;

                    // парсинг ответа
                    if (whc != null)
                    {
                        try
                        {
                            string location = whc["Location"];

                            if (location.IndexOf("sid=-1") != -1)
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else if (location.IndexOf("sid=-2") != -1)
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else if (location.IndexOf("sid=-3") != -1)
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }
                            else if (location.IndexOf("sid=-4") != -1)
                            {
                                throw new VKException(ExceptionMessage.IncorrectLoginOrPassword);
                            }

                            authLoginResponse.session_key = location.Substring(location.IndexOf("sid=") + "sid=".Length);
                        }
                        catch (ArgumentException)
                        {
                            authLoginResponse.session_key = string.Empty;
                        }

                        try
                        {
                            string cookie = whc["Set-Cookie"];

                            int startId = cookie.IndexOf("remixmid=") + "remixmid=".Length;
                            int idLength = 0;

                            cookie = cookie.Remove(0, startId);

                            for (int i = 0; i < cookie.Length; i++)
                            {
                                if (cookie[i] != ';')
                                {
                                    idLength++;
                                }
                                else
                                {
                                    break;
                                }
                            }

                            authLoginResponse.uid = cookie.Substring(0, idLength);
                        }
                        catch (ArgumentException)
                        {
                            authLoginResponse.uid = string.Empty;
                        }

                        if (isRemember)
                        {
                            try
                            {
                                string cookie = whc["Set-Cookie"];

                                int startId = cookie.IndexOf("remixpassword="******"remixpassword="******"AuthLogin ObjectDisposedException");

                return null;
            }
            finally
            {
                request.Abort();
            }

            return null;
        }