Example #1
0
 public AuthenticationApiError(string reason, AuthenticationErrorCode errorCode)
     : base(
         "An error occurred while authenticating the request.",
         ApiErrorCode.AuthenticationFailed
         )
 {
     Details = new ErrorDetails(reason, errorCode);
 }
 public AuthenticationException(AuthenticationErrorCode errorCode, string message, Exception innerException) : base(message, innerException)
 {
     ErrorCode = errorCode;
 }
 public AuthenticationException(AuthenticationErrorCode errorCode, string message) : this(errorCode, message, null) { }
Example #4
0
 public AuthenticationException(string message, AuthenticationErrorCode errorCode)
     : base(message)
 {
     ErrorCode = errorCode;
 }
Example #5
0
        /// <summary>
        /// Attempts to get or update the user authentication credentials that are required for web service calls.
        /// </summary>
        /// <exception cref="AuthenticationException">The <see cref="Token"/> is not set or the authentication system returned an error. Check the ErrorCode property for details.</exception>
        public void UpdateCredentials()
        {
            System.Xml.XPath.XPathDocument  xpDoc;
            System.Xml.XPath.XPathNavigator navigator;
            System.Xml.XPath.XPathNavigator node;

            if (this._token.Length == 0)
            {
                throw new AuthenticationException(Properties.Resources.ErrorTokenNotSet, AuthenticationErrorCode.TokenInvalid);
            }

            // Make a call to the authentication service to retrieve the WSSID and cookie
            try
            {
                // Clear current authentication details
                _wssId   = "";
                _cookies = "";

                using (System.IO.Stream dataStream = GetServiceStream(GetTokenLogOnAddress(), false))
                {
                    xpDoc = new System.Xml.XPath.XPathDocument(dataStream);
                }

                // Attempt to select "/BBAuthTokenLoginResponse/Success"
                navigator = xpDoc.CreateNavigator();

                node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Success");

                if (node != null)
                {
                    // Get cookies
                    node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Success/Cookie");
                    if (node != null)
                    {
                        _cookies = node.Value;
                    }
                    if (_cookies.Length != 0)
                    {
                        _cookies = _cookies.Replace(";", ",");
                    }
                    else
                    {
                        // Raise custom error
                        throw new AuthenticationException(Properties.Resources.ErrorCookieParse, AuthenticationErrorCode.Other);
                    }

                    // Get WSSID
                    node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Success/WSSID");
                    if (node != null)
                    {
                        _wssId = node.Value;
                    }
                    else
                    {
                        // Raise custom error
                        throw new AuthenticationException(Properties.Resources.ErrorWssIdNotSet, AuthenticationErrorCode.Other);
                    }

                    // Get timeout value of the cookie and WSSID
                    int timeout = 0;
                    node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Success/Timeout");
                    if (node != null)
                    {
                        try
                        {
                            // With .NET 2.0 we could use node.ValueAsInt
                            timeout = int.Parse(node.Value, System.Globalization.CultureInfo.InvariantCulture);
                        }
                        catch (FormatException)
                        {
                            // Raise custom error
                            throw new AuthenticationException(Properties.Resources.ErrorTimeoutParse, AuthenticationErrorCode.Other);
                        }
                        catch (OverflowException)
                        {
                            // Raise custom error
                            throw new AuthenticationException(Properties.Resources.ErrorTimeoutParse, AuthenticationErrorCode.Other);
                        }
                        catch (InvalidCastException)
                        {
                            // Raise custom error
                            throw new AuthenticationException(Properties.Resources.ErrorTimeoutParse, AuthenticationErrorCode.Other);
                        }
                    }
                    else
                    {
                        // Raise custom error
                        throw new AuthenticationException(Properties.Resources.ErrorTimeoutParse, AuthenticationErrorCode.Other);
                    }
                    _validUntil = DateTime.Now.AddSeconds(timeout);
                }
                else
                {
                    AuthenticationErrorCode errorCode = AuthenticationErrorCode.Unknown;
                    string errorCodeText = "";
                    string errorDesc     = Properties.Resources.ErrorUnknown;

                    node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Error");

                    if (node != null)
                    {
                        // Get error code
                        node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Error/ErrorCode");
                        if (node != null)
                        {
                            errorCodeText = node.Value;
                        }

                        if (errorCodeText.Length != 0)
                        {
                            try
                            {
                                if (Enum.IsDefined(typeof(AuthenticationErrorCode), int.Parse(errorCodeText, System.Globalization.CultureInfo.InvariantCulture)) == true)
                                {
                                    errorCode = (AuthenticationErrorCode)Enum.Parse(typeof(AuthenticationErrorCode), errorCodeText, true);
                                }
                            }
                            catch (ArgumentException) { }
                            catch (FormatException) { }
                        }

                        // Get error description
                        node = SelectSingleNode(navigator, "/BBAuthTokenLoginResponse/Error/ErrorDescription");
                        if (node != null)
                        {
                            errorDesc = node.Value;
                        }
                    }

                    // Raise error
                    throw new AuthenticationException(errorDesc, errorCode);
                }
            }
            catch (System.Net.WebException wex)
            {
                // Try to retrieve more information about the network error
                string errorDesc = Properties.Resources.ErrorNetwork;
                System.Net.HttpWebResponse errorResponse = null;

                try
                {
                    errorResponse = wex.Response as System.Net.HttpWebResponse;
                    if (errorResponse != null)
                    {
                        errorDesc = errorResponse.StatusDescription + " (" + ((int)errorResponse.StatusCode).ToString(System.Globalization.CultureInfo.InvariantCulture) + ")";
                    }
                }
                catch { }
                finally
                {
                    if (errorResponse != null)
                    {
                        errorResponse.Close();
                    }
                }

                // Raise error
                throw new AuthenticationException(errorDesc, wex, AuthenticationErrorCode.NetworkError);
            }
        }
Example #6
0
 public ErrorDetails(string reason, AuthenticationErrorCode errorCode)
 {
     Reason    = reason;
     ErrorCode = errorCode;
 }