public void ProcessRequest(HttpContext context)
        {
            string result = null;

            try
            {
                UserManagementService.Activate(context.Request.Params[UserManagementService.ActivationParameter]);

                var response = new SiteResponse()
                {
                    response = "Your account has been activated.",
                    status = SiteResponse.Status.Success,
                    syncKey = "aSyncKey"
                };

                result = JSONHelper.Serialize<SiteResponse>(response);
            }
            catch (UserManagementServiceException umse)
            {
                ExceptionHelper.Code exceptionCode = ExceptionHelper.Code.UnexpectedException;
                string message = umse.Message;

                switch (umse.Code)
                {
                    case UserManagementServiceException.ErrorCode.UnexpectedError:
                        break;
                    case UserManagementServiceException.ErrorCode.ObjectNotFound:
                        exceptionCode = ExceptionHelper.Code.InvalidLogin;
                        break;
                    case UserManagementServiceException.ErrorCode.InvalidOperationOnResource:
                        exceptionCode = ExceptionHelper.Code.InvalidOperation;
                        break;
                    case UserManagementServiceException.ErrorCode.AccessDenied:
                        exceptionCode = ExceptionHelper.Code.AccessDenied;
                        break;
                    case UserManagementServiceException.ErrorCode.CouldNotConnectToDatabase:
                        message = "Could not connect to the database. " + message;
                        break;
                    default:
                        message = "Unknown ErrorCode: " + umse.Code + ". Message: " + message;
                        break;
                }

                result = JSONHelper.Serialize(ExceptionHelper.Handle(umse, exceptionCode, message, log));
            }
            catch (Exception e)
            {
                result = JSONHelper.Serialize(ExceptionHelper.Handle(e, log));
            }
            finally
            {
                context.Response.ContentType = MediaTypeNames.Text.Plain;
                context.Response.Write(result);
            }
        }
        private SiteResponse Buy(HttpContext context)
        {
            string loginToken = context.Param(SiteParameters.LOGIN_TOKEN);
            var paymentType = context.Param<PaymentProcessorType>(SiteParameters.PAYMENT_TYPE);
            string productId = context.Param(SiteParameters.PRODUCT_ID);
            var lt = JSONHelper.Deserialize<LoginToken>(loginToken);

            VerifySession(lt);

            var processor = PaymentProcessorFactory.Create(paymentType);
            string customerId = GetCustomerId(lt).ToString();
            decimal paymentAmount = GetPaymentAmount(productId);

            var uri = processor.Buy(customerId, productId, paymentAmount);

            var response = new SiteResponse()
            {
                response = uri,
                status = SiteResponse.Status.Success,
                syncKey = "aSyncKey"
            };

            return response;
        }
        public void ProcessRequest(HttpContext context)
        {
            string result = null;

            try
            {
                string emailAsString = context.Param(SiteParameters.EMAIL);
                MailAddress email = new MailAddress(emailAsString);
                string username = context.Param(SiteParameters.USER_NAME);
                string password = context.Param(SiteParameters.PASSWORD);
                var currentSite = new StringBuilder(context.Request.Url.GetLeftPart(UriPartial.Authority));

                // fail fast if either the username or email already exist in the system.
                if (SessionUtil.IsUsernameTaken(username))
                {
                    string usernameTakenResponseMessage = "The username " + username + " already exists in this system.";
                    throw new ApplicationException(usernameTakenResponseMessage);
                }

                if (SessionUtil.IsEmailTaken(emailAsString))
                {
                    string emailTakenResponseMessage = "The e-mail " + emailAsString + " already exists in this system.";
                    throw new ApplicationException(emailTakenResponseMessage);
                }

                for (int i = 0; i < context.Request.Url.Segments.Length - 1; i++)
                    currentSite.Append(context.Request.Url.Segments[i]);

                currentSite.Append("Activate.aspx");
                Uri callbackLink = new Uri(currentSite.ToString());

                UserManagementService.SignUp(email, username, password, callbackLink);

                var response = new SiteResponse()
                {
                    response = "Please check email for activation link.",
                    status = SiteResponse.Status.Success,
                    syncKey = "aSyncKey"
                };

                result = JSONHelper.Serialize<SiteResponse>(response);
            }
            catch (UserManagementServiceException umse)
            {
                ExceptionHelper.Code exceptionCode = ExceptionHelper.Code.UnexpectedException;
                string message = umse.Message;

                switch (umse.Code)
                {
                    case UserManagementServiceException.ErrorCode.UnexpectedError:
                        break;
                    case UserManagementServiceException.ErrorCode.ObjectNotFound:
                        exceptionCode = ExceptionHelper.Code.InvalidLogin;
                        break;
                    case UserManagementServiceException.ErrorCode.InvalidOperationOnResource:
                        exceptionCode = ExceptionHelper.Code.InvalidOperation;
                        break;
                    case UserManagementServiceException.ErrorCode.AccessDenied:
                        exceptionCode = ExceptionHelper.Code.AccessDenied;
                        break;
                    case UserManagementServiceException.ErrorCode.CouldNotConnectToDatabase:
                        message = "Could not connect to the database. " + message;
                        break;
                    default:
                        message = "Unknown ErrorCode: " + umse.Code + ". Message: " + message;
                        break;
                }

                result = JSONHelper.Serialize(ExceptionHelper.Handle(umse, exceptionCode, message, log));
            }
            catch (Exception e)
            {
                result = JSONHelper.Serialize(ExceptionHelper.Handle(e, log));
            }
            finally
            {
                context.Response.ContentType = MediaTypeNames.Text.Plain;
                context.Response.Write(result);
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            string result = null;

            try
            {
                SiteResponse response;

                var lt = JSONHelper.Deserialize<LoginToken>(context.Param(SiteParameters.LOGIN_TOKEN));
                UserManagementService.ValidateLoginToken(lt);
                var user = UserManagementService.FindUserWithLoginToken(lt);
                var customer = CustomerManagementService.FindCustomerWithUserId(user.Id);

                response = new SiteResponse
                {
                    response = new GetCustomerResponse
                    {
                        customer = customer,
                        username = user.AliasName
                    },
                    status = SiteResponse.Status.Success,
                    syncKey = "are we using this?"
                };

                result = JSONHelper.Serialize<SiteResponse>(response, new[] { typeof(GetCustomerResponse) });
            }
            catch (UserManagementServiceException umse)
            {
                ExceptionHelper.Code exceptionCode = ExceptionHelper.Code.UnexpectedException;
                string message = umse.Message;

                switch (umse.Code)
                {
                    case UserManagementServiceException.ErrorCode.UnexpectedError:
                        break;
                    case UserManagementServiceException.ErrorCode.ObjectNotFound:
                        exceptionCode = ExceptionHelper.Code.InvalidLogin;
                        break;
                    case UserManagementServiceException.ErrorCode.InvalidOperationOnResource:
                        exceptionCode = ExceptionHelper.Code.InvalidOperation;
                        break;
                    case UserManagementServiceException.ErrorCode.AccessDenied:
                        exceptionCode = ExceptionHelper.Code.AccessDenied;
                        break;
                    case UserManagementServiceException.ErrorCode.CouldNotConnectToDatabase:
                        message = "Could not connect to the database. " + message;
                        break;
                    default:
                        message = "Unknown ErrorCode: " + umse.Code + ". Message: " + message;
                        break;
                }

                result = JSONHelper.Serialize(ExceptionHelper.Handle(umse, exceptionCode, message, log));
            }
            catch (Exception e)
            {
                result = JSONHelper.Serialize(ExceptionHelper.Handle(e, log));
            }
            finally
            {
                context.Response.ContentType = MediaTypeNames.Text.Plain;
                context.Response.Write(result);
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            string result = null;

            try
            {
                SiteResponse response;
                string username = context.Param(SiteParameters.USER_NAME);
                string email = context.Param(SiteParameters.EMAIL);
                string password = context.Param(SiteParameters.PASSWORD);
                var appKey = new AppKey(context.Param(SiteParameters.APP_KEY));

                var user = UserManagementService.CreateUser(username, email, password, appKey);

                response = new SiteResponse
                {
                    response = user,
                    status = SiteResponse.Status.Success,
                    syncKey = "are we using this?"
                };

                result = JSONHelper.Serialize<SiteResponse>(response, new []{typeof(User)});
            }
            catch (UserManagementServiceException umse)
            {
                ExceptionHelper.Code exceptionCode = ExceptionHelper.Code.UnexpectedException;
                string message = umse.Message;

                switch (umse.Code)
                {
                    case UserManagementServiceException.ErrorCode.UnexpectedError:
                        break;
                    case UserManagementServiceException.ErrorCode.ObjectNotFound:
                        exceptionCode = ExceptionHelper.Code.InvalidLogin;
                        break;
                    case UserManagementServiceException.ErrorCode.InvalidOperationOnResource:
                        exceptionCode = ExceptionHelper.Code.InvalidOperation;
                        break;
                    case UserManagementServiceException.ErrorCode.AccessDenied:
                        exceptionCode = ExceptionHelper.Code.AccessDenied;
                        break;
                    case UserManagementServiceException.ErrorCode.CouldNotConnectToDatabase:
                        message = "Could not connect to the database. " + message;
                        break;
                    default:
                        message = "Unknown ErrorCode: " + umse.Code + ". Message: " + message;
                        break;
                }

                result = JSONHelper.Serialize(ExceptionHelper.Handle(umse, exceptionCode, message, log));
            }
            catch (Exception e)
            {
                result = JSONHelper.Serialize(ExceptionHelper.Handle(e, log));
            }
            finally
            {
                context.Response.ContentType = MediaTypeNames.Text.Plain;
                context.Response.Write(result);
            }
        }