예제 #1
0
        /// <summary>
        /// Login into the web server using OneAll system in order to be ready to send web service requests
        /// </summary>
        /// <param name="identToken"></param>
        public UserClient LoginWithOneAll(string identToken)
        {
            if (identToken.IsNullOrWhiteSpaceOrEOF())
            {
                return(new UserClient(false));
            }

            string postData = "identToken=" + identToken;

            // now post to the login form
#if USE_OAUTH
            //check the validity of Access Token:
            if (AccessTokenInvalid())
            {
                //maybe in the future we can also refresh the token instead of creating a new one after the expiration...
                if (!ReceiveAccessToken(User.ONEALL_FAKE_USERNAME, identToken))
                {
                    return(new UserClient(false));
                }
            }

            string loginUrl = string.Format("{0}{1}?{2}", BaseWS.Server, OAUTH_ONEALL_LOGIN_URL, postData);
            string response = SendGetRequest(loginUrl, true);
#else
            string loginUrl = string.Format("{0}{1}", BaseWS.Server, LOGIN_ONEALL_URL);
            string response = SendPostRequest(loginUrl, postData, false);
#endif

            S2CResBaseEntity <UserClient> resp = null;
            if (string.IsNullOrEmpty(response))
            {
                resp = new S2CResBaseEntity <UserClient>(-1, ErrorCodes.COMMUNICATION_ERROR, null);
            }
            else
            {
                resp = S2CSerializer.DeserializeBaseEntity <UserClient>(response, m_requestFormat);
            }

            //check that the authentication request has been correctly received:
            m_isLogged = ((m_cookies != null) && (m_cookies.Count > 0) && (resp != null) &&
                          (resp.Status == ErrorCodes.OK) && (resp.Data != null));

            if ((resp != null) && (resp.Status == ErrorCodes.FAIL))
            {
                return(new UserClient(false));
            }

            if (resp == null)
            {
                return(null);
            }
            else
            {
                return(resp.Data);
            }
        }
예제 #2
0
        /// <summary>
        /// Login into the web server in order to be ready to send web service requests
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public UserClient Login(string username, string password)
        {
            string viewStateString = string.Empty;

#if NEED_VIEW_STATE
            // first, request the login form to get the viewstate value
            HttpWebRequest viewStateRequest = WebRequest.Create(loginUrl) as HttpWebRequest;
            viewStateRequest.Accept = "application/xhtml+xml";

            StreamReader responseReader = new StreamReader(viewStateRequest.GetResponse().GetResponseStream());
            string       responseData   = responseReader.ReadToEnd();
            responseReader.Close();

            // extract the viewstate value and build out POST data
            string viewState = ExtractViewState(responseData);
            if (viewState != string.Empty)
            {
                viewStateString = string.Format("__VIEWSTATE={0}&", viewState);
            }
#endif
            string encryptedPsw = password.ComputeMD5HashSQLServerCompliant();
            string postData     = string.Format("{0}{1}={2}&{3}={4}&{5}={6}",
                                                viewStateString,
                                                USERNAME_TEXTBOX_NAME, HttpUtility.UrlEncode(username),
                                                PASSWORD_TEXTBOX_NAME, HttpUtility.UrlEncode(encryptedPsw),
                                                LOGIN_BUTTON_NAME, LOGIN_BUTTON_VALUE);

            // now post to the login form
#if USE_OAUTH
            //check the validity of Access Token:
            if (AccessTokenInvalid())
            {
                //maybe in the future we can also refresh the token instead of creating a new one after the expiration...
                if (!ReceiveAccessToken(username, encryptedPsw))
                {
                    return(new UserClient(false));
                }
            }

            string loginUrl = string.Format("{0}{1}?{2}", BaseWS.Server, OAUTH_LOGIN_URL, postData);
            string response = SendGetRequest(loginUrl, true);
#else
            string loginUrl = string.Format("{0}{1}", BaseWS.Server, LOGIN_URL);
            string response = SendPostRequest(loginUrl, postData, false);
#endif

            S2CResBaseEntity <UserClient> resp = null;
            if (string.IsNullOrEmpty(response))
            {
                resp = new S2CResBaseEntity <UserClient>(-1, ErrorCodes.COMMUNICATION_ERROR, null);
            }
            else
            {
                resp = S2CSerializer.DeserializeBaseEntity <UserClient>(response, m_requestFormat);
            }

            //check that the authentication request has been correctly received:
            m_isLogged = ((m_cookies != null) && (m_cookies.Count > 0) && (resp != null) &&
                          (resp.Status == ErrorCodes.OK) && (resp.Data != null));

            if ((resp != null) && (resp.Status == ErrorCodes.FAIL))
            {
                return(new UserClient(false));
            }

            if (resp == null)
            {
                return(null);
            }
            else
            {
                return(resp.Data);
            }
        }