private void AuthenticateSecondAuth(IHttpRequest req, IHasMobileRequestHead mobileRequest, string auth, string sauth)
        {
            if (mobileRequest.HasExtensionData(ServiceUtils.MobileAuthTokenExtensionKey))
            {
                throw new MobileRequestFilterException(
                          "OperationName: " + req.OperationName + ". Request Head Extension fileds have had the authenticated auth. Request Head has bad data or MobileRequestFilter has been applied.");
            }
            if (mobileRequest.HasExtensionData(MobileRequestUtils.MobileSecondAuthExtensionKey))
            {
                throw new MobileRequestFilterException(
                          "OperationName: " + req.OperationName + ". Request Head Extension fileds have had the authenticated sauth. Request Head has bad data or MobileRequestFilter has been applied.");
            }

            if (!string.IsNullOrWhiteSpace(sauth))
            {
                CheckSecondTokenResponse checkSecondTokenResponse = null;
                try
                {
                    checkSecondTokenResponse = _secondAuthServiceClient.CheckSecondToken(
                        new CheckSecondTokenRequest()
                    {
                        Token = sauth,
                        Auth  = auth
                    });
                }
                catch (CServiceException ex)
                {
                    if (IsOnDemandMode)
                    {
                        return;
                    }
                    throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Error happened when doing second Authorization.", ex);
                }
                catch (Exception ex)
                {
                    if (IsOnDemandMode)
                    {
                        return;
                    }
                    throw new Exception("OperationName: " + req.OperationName + ". Error happened when connecting to Authorization service: " + ex.Message, ex);
                }

                if (IsOnDemandMode && checkSecondTokenResponse.ReturnCode != 0)
                {
                    return;
                }
                switch (checkSecondTokenResponse.ReturnCode)
                {
                case 0:
                    break;

                case 101:
                    throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                           + ". No second auth for authentication. Message: " + checkSecondTokenResponse.Message + ". \nReference document: " + AuthWiki);

                case 102:
                    throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                           + ". No first auth for authentication. Message: " + checkSecondTokenResponse.Message + ". \nReference document: " + AuthWiki);

                case 201:
                    throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                           + ". " + ServiceUtils.InvalidTokenExceptionMessage + " Message: " + checkSecondTokenResponse.Message + ". \nReference document: " + AuthWiki);

                case 900:
                    throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                           + ". Authorization Service Internal Exception. Message: " + checkSecondTokenResponse.Message + ". \nReference document: " + AuthWiki);

                default:
                    throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Unknown Authorization Service Return Code: "
                                                           + checkSecondTokenResponse.ReturnCode + ". Message: " + checkSecondTokenResponse.Message + ". \nReference document: " + AuthWiki);
                }

                if (string.IsNullOrWhiteSpace(checkSecondTokenResponse.Uid))
                {
                    if (IsOnDemandMode)
                    {
                        return;
                    }

                    string format  = "OperationName: {0}. Empty uid was returned by Authorization service. IsNew: {1}, Message: {2}";
                    string message = string.Format(format, req.OperationName, checkSecondTokenResponse.IsNew, checkSecondTokenResponse.Message);
                    throw new MobileRequestFilterException(message);
                }

                if (string.IsNullOrWhiteSpace(checkSecondTokenResponse.Token))
                {
                    if (IsOnDemandMode)
                    {
                        return;
                    }
                    string format  = "OperationName: {0}. Empty token was returned by Authorization service. IsNew: {1}, Message: {2}";
                    string message = string.Format(format, req.OperationName, checkSecondTokenResponse.IsNew, checkSecondTokenResponse.Message);
                    throw new MobileRequestFilterException(message);
                }

                if (!string.IsNullOrWhiteSpace(auth))
                {
                    mobileRequest.AddExtensionData(ServiceUtils.MobileAuthTokenExtensionKey, auth);
                }
                mobileRequest.AddExtensionData(MobileRequestUtils.MobileSecondAuthExtensionKey, checkSecondTokenResponse.Token);
                mobileRequest.AddExtensionData(ServiceUtils.MobileUserIdExtensionKey, checkSecondTokenResponse.Uid);
                return;
            }

            GenSecondAuthorizationTokenResponse genSecondAuthorizationTokenResponse = null;

            try
            {
                genSecondAuthorizationTokenResponse = _secondAuthServiceClient.GenSecondAuthorizationToken(
                    new GenSecondAuthorizationTokenRequest()
                {
                    Auth = auth
                });
            }
            catch (CServiceException ex)
            {
                if (IsOnDemandMode)
                {
                    return;
                }
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Error happened when doing GenSecondAuthorizationToken.", ex);
            }
            catch (Exception ex)
            {
                if (IsOnDemandMode)
                {
                    return;
                }
                throw new Exception("OperationName: " + req.OperationName + ". Error happened when connecting to Authorization service: " + ex.Message, ex);
            }

            if (IsOnDemandMode && genSecondAuthorizationTokenResponse.ReturnCode != 0)
            {
                return;
            }
            switch (genSecondAuthorizationTokenResponse.ReturnCode)
            {
            case 0:
                break;

            case 101:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                       + ". No auth for authentication. Message: " + genSecondAuthorizationTokenResponse.Message + ". \nReference document: " + AuthWiki);

            case 201:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                       + ". " + ServiceUtils.InvalidTokenExceptionMessage + " Message: " + genSecondAuthorizationTokenResponse.Message + ". \nReference document: " + AuthWiki);

            case 900:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName
                                                       + ". Authorization Service Internal Exception. Message: " + genSecondAuthorizationTokenResponse.Message + ". \nReference document: " + AuthWiki);

            default:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Unknown Authorization Service Return Code: "
                                                       + genSecondAuthorizationTokenResponse.ReturnCode + ". Message: " + genSecondAuthorizationTokenResponse.Message + ". \nReference document: " + AuthWiki);
            }

            if (string.IsNullOrWhiteSpace(genSecondAuthorizationTokenResponse.Token))
            {
                if (IsOnDemandMode)
                {
                    return;
                }
                string format  = "OperationName: {0}. Empty new second auth token was returned by Authorization service. ExpiredTime: {1}, Message: {2}";
                string message = string.Format(format, req.OperationName, genSecondAuthorizationTokenResponse.ExpiredTime, genSecondAuthorizationTokenResponse.Message);
                throw new MobileRequestFilterException(message);
            }

            mobileRequest.AddExtensionData(ServiceUtils.MobileAuthTokenExtensionKey, auth);
            mobileRequest.AddExtensionData(MobileRequestUtils.MobileSecondAuthExtensionKey, genSecondAuthorizationTokenResponse.Token);
            mobileRequest.AddExtensionData(ServiceUtils.MobileUserIdExtensionKey, genSecondAuthorizationTokenResponse.Uid);
        }
        protected virtual void AuthenticateRequest(IHttpRequest req, IHasMobileRequestHead mobileRequest, string auth)
        {
            if (mobileRequest.HasExtensionData(ServiceUtils.MobileAuthTokenExtensionKey))
            {
                throw new MobileRequestFilterException(
                          "OperationName: " + req.OperationName + ". Request Head Extension fileds have had the authenticated auth. Request Head has bad data or MobileRequestFilter has been applied.");
            }

            ValidateAndGetNewTokenResponse response = null;

            try
            {
                response = _mobileAuthServiceClient.ValidateAndGetNewToken(new ValidateAndGetNewTokenRequest()
                {
                    Token = auth
                });
            }
            catch (CServiceException ex)
            {
                if (IsOnDemandMode)
                {
                    return;
                }
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Error happened when doing Auth.", ex);
            }
            catch (Exception ex)
            {
                if (IsOnDemandMode)
                {
                    return;
                }
                throw new Exception("OperationName: " + req.OperationName + ". Error happened when connecting to mobile auth service: " + ex.Message, ex);
            }

            if (IsOnDemandMode && response.ReturnCode != 0)
            {
                return;
            }
            switch (response.ReturnCode)
            {
            case 0:
                break;

            case 1001:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". No auth for authentication. Message: " + response.Message + ". \nReference document: " + AuthWiki);

            case 2001:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". " + ServiceUtils.InvalidTokenExceptionMessage + " Message: " + response.Message + ". \nReference document: " + AuthWiki);

            case 9000:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Mobile Auth Service Internal Exception. Message: " + response.Message + ". \nReference document: " + AuthWiki);

            default:
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Unknown Auth Service Return Code: " + response.ReturnCode + ". Message: " + response.Message + ". \nReference document: " + AuthWiki);
            }

            if (string.IsNullOrWhiteSpace(response.NewToken))
            {
                if (IsOnDemandMode)
                {
                    return;
                }
                throw new MobileRequestFilterException("OperationName: " + req.OperationName + ". Empty new auth was returned by MobileAuthService.");
            }

            AddAuthResponseData(req, mobileRequest, response);
        }