private void DoExistingUserLogic(Guid userGuid)
        {
            // user found so login if allowed
            SiteUser user = new SiteUser(siteSettings, userGuid);

            bool canLogin = true;

            if (
                (siteSettings.UseSecureRegistration) &&
                (user.RegisterConfirmGuid != Guid.Empty)
                )
            {
                Notification.SendRegistrationConfirmationLink(
                    SiteUtils.GetSmtpSettings(),
                    ResourceHelper.GetMessageTemplate("RegisterConfirmEmailMessage.config"),
                    siteSettings.DefaultEmailFromAddress,
                    siteSettings.DefaultFromEmailAlias,
                    user.Email,
                    siteSettings.SiteName,
                    WebUtils.GetSiteRoot() + "/ConfirmRegistration.aspx?ticket=" +
                    user.RegisterConfirmGuid.ToString());


                log.Info("User " + user.Name + " tried to login but email address is not confirmed.");

                canLogin = false;
            }

            if (user.IsLockedOut)
            {
                log.Info("User " + user.Name + " tried to login but account is locked.");

                canLogin = false;
            }

            if ((siteSettings.RequireApprovalBeforeLogin) && (!user.ApprovedForLogin))
            {
                log.Info("User " + user.Name + " tried to login but account is not approved yet.");
                canLogin = false;
            }

            if (canLogin)
            {
                if (siteSettings.UseEmailForLogin)
                {
                    FormsAuthentication.SetAuthCookie(
                        user.Email, persistCookie);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(
                        user.LoginName, persistCookie);
                }

                if (user.LiveMessengerDelegationToken.Length > 0)
                {
                    WindowsLiveMessenger m     = new WindowsLiveMessenger(windowsLive);
                    ConsentToken         token = m.DecodeToken(user.LiveMessengerDelegationToken);
                    token = m.RefreshConsent(token);
                    if (token != null)
                    {
                        CookieHelper.SetCookie(consentTokenCookie, token.Token);
                        CookieHelper.SetCookie(delegationTokenCookie, token.DelegationToken);
                    }
                }

                if (WebConfigSettings.UseFolderBasedMultiTenants)
                {
                    string cookieName = "siteguid" + siteSettings.SiteGuid;
                    CookieHelper.SetCookie(cookieName, user.UserGuid.ToString(), persistCookie);
                }

                if (user.UserId > -1 && siteSettings.AllowUserSkins && user.Skin.Length > 0)
                {
                    SiteUtils.SetSkinCookie(user);
                }


                user.UpdateLastLoginTime();

                // track user ip address
                UserLocation userLocation = new UserLocation(user.UserGuid, SiteUtils.GetIP4Address());
                userLocation.SiteGuid = siteSettings.SiteGuid;
                userLocation.Hostname = Page.Request.UserHostName;
                userLocation.Save();

                string redirectUrl = GetRedirectPath();
                CookieHelper.ExpireCookie(returnUrlCookieName);

                UserSignInEventArgs u = new UserSignInEventArgs(user);
                OnUserSignIn(u);

                //WebUtils.SetupRedirect(this, redirectUrl);
                Response.Redirect(redirectUrl);
                return;
            }
            else
            {
                // redirect to login
                // need to make login page show
                // reason for failure
                //WebUtils.SetupRedirect(this, LoginPage);
                Response.Redirect(LoginPage);
            }
        }
        private void HandleConsent()
        {
            //http://msdn.microsoft.com/en-us/library/cc287661.aspx

            ConsentToken consent = null;
            messengerApplication = new WindowsLiveMessenger(windowsLive);

            if (String.IsNullOrEmpty(this.Request.Params["ConsentToken"]))
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("usertoken was null");
                }
                consent = this.messengerApplication.HandleConsentResponse(this.Request.Params);
            }
            else
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("usertoken was not null");
                }
                consent = this.messengerApplication.DecodeToken(this.Request.Params["ConsentToken"]);
            }

            //the windows live id on the siteUser is I think the same thing as usertoken
            // it just needs to be decoded

            if (consent == null)
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("HandleConsentCompleted consent was null");
                }
                WebUtils.SetupRedirect(this, siteRoot);
                return;
            }

            if (WebConfigSettings.DebugWindowsLive)
            {
                if (consent.IsValid())
                {
                    log.Info("HandleConsent obtained valid consent");
                }
                else
                {
                    log.Info("HandleConsent obtained invalid consent");
                }

            }

            CookieHelper.SetCookie(consentTokenCookie, consent.Token);
            CookieHelper.SetCookie(delegationTokenCookie, consent.DelegationToken);

            SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
            if ((currentUser != null) && (currentUser.UserGuid != Guid.Empty))
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("HandleConsent obtained siteUser");
                }

                currentUser.LiveMessengerId = consent.CID;
                currentUser.LiveMessengerDelegationToken = consent.Token;

                //if (!String.IsNullOrEmpty(Request.Params["ConsentToken"]))
                //{
                //    currentUser.LiveMessengerDelegationToken = Request.Params["ConsentToken"];
                //}

                currentUser.Save();

                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("HandleConsent saved CID " + consent.CID + " for user " + currentUser.Email + " " + currentUser.Name);

                }

                WebUtils.SetupRedirect(this, siteRoot + "/Secure/UserProfile.aspx");
                return;
            }

            if (WebConfigSettings.DebugWindowsLive)
            {
                log.Info("HandleConsent redirecting to site root");
            }

            WebUtils.SetupRedirect(this, siteRoot);
        }
        private void HandleConsent()
        {
            //http://msdn.microsoft.com/en-us/library/cc287661.aspx

            ConsentToken consent = null;

            messengerApplication = new WindowsLiveMessenger(windowsLive);



            if (String.IsNullOrEmpty(this.Request.Params["ConsentToken"]))
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("usertoken was null");
                }
                consent = this.messengerApplication.HandleConsentResponse(this.Request.Params);
            }
            else
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("usertoken was not null");
                }
                consent = this.messengerApplication.DecodeToken(this.Request.Params["ConsentToken"]);
            }

            //the windows live id on the siteUser is I think the same thing as usertoken
            // it just needs to be decoded

            if (consent == null)
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("HandleConsentCompleted consent was null");
                }
                WebUtils.SetupRedirect(this, siteRoot);
                return;
            }

            if (WebConfigSettings.DebugWindowsLive)
            {
                if (consent.IsValid())
                {
                    log.Info("HandleConsent obtained valid consent");
                }
                else
                {
                    log.Info("HandleConsent obtained invalid consent");
                }
            }


            CookieHelper.SetCookie(consentTokenCookie, consent.Token);
            CookieHelper.SetCookie(delegationTokenCookie, consent.DelegationToken);


            SiteUser currentUser = SiteUtils.GetCurrentSiteUser();

            if ((currentUser != null) && (currentUser.UserGuid != Guid.Empty))
            {
                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("HandleConsent obtained siteUser");
                }

                currentUser.LiveMessengerId = consent.CID;
                currentUser.LiveMessengerDelegationToken = consent.Token;

                //if (!String.IsNullOrEmpty(Request.Params["ConsentToken"]))
                //{
                //    currentUser.LiveMessengerDelegationToken = Request.Params["ConsentToken"];
                //}

                currentUser.Save();

                if (WebConfigSettings.DebugWindowsLive)
                {
                    log.Info("HandleConsent saved CID " + consent.CID + " for user " + currentUser.Email + " " + currentUser.Name);
                }

                WebUtils.SetupRedirect(this, siteRoot + "/Secure/UserProfile.aspx");
                return;
            }

            if (WebConfigSettings.DebugWindowsLive)
            {
                log.Info("HandleConsent redirecting to site root");
            }


            WebUtils.SetupRedirect(this, siteRoot);
        }
        private void DoExistingUserLogic(Guid userGuid)
        {
            // user found so login if allowed
            SiteUser user = new SiteUser(siteSettings, userGuid);

            bool canLogin = true;

            if (
                (siteSettings.UseSecureRegistration)
                && (user.RegisterConfirmGuid != Guid.Empty)
                )
            {
                Notification.SendRegistrationConfirmationLink(
                    SiteUtils.GetSmtpSettings(),
                    ResourceHelper.GetMessageTemplate("RegisterConfirmEmailMessage.config"),
                    siteSettings.DefaultEmailFromAddress,
                    siteSettings.DefaultFromEmailAlias,
                    user.Email,
                    siteSettings.SiteName,
                    WebUtils.GetSiteRoot() + "/ConfirmRegistration.aspx?ticket=" +
                    user.RegisterConfirmGuid.ToString());

                log.Info("User " + user.Name + " tried to login but email address is not confirmed.");

                canLogin = false;
            }

            if (user.IsLockedOut)
            {

                log.Info("User " + user.Name + " tried to login but account is locked.");

                canLogin = false;
            }

            if ((siteSettings.RequireApprovalBeforeLogin) && (!user.ApprovedForLogin))
            {
                log.Info("User " + user.Name + " tried to login but account is not approved yet.");
                canLogin = false;
            }

            if (canLogin)
            {
                if (siteSettings.UseEmailForLogin)
                {
                    FormsAuthentication.SetAuthCookie(
                        user.Email, persistCookie);

                }
                else
                {
                    FormsAuthentication.SetAuthCookie(
                        user.LoginName, persistCookie);

                }

                if (user.LiveMessengerDelegationToken.Length > 0)
                {
                    WindowsLiveMessenger m = new WindowsLiveMessenger(windowsLive);
                    ConsentToken token = m.DecodeToken(user.LiveMessengerDelegationToken);
                    token = m.RefreshConsent(token);
                    if (token != null)
                    {
                        CookieHelper.SetCookie(consentTokenCookie, token.Token);
                        CookieHelper.SetCookie(delegationTokenCookie, token.DelegationToken);
                    }

                }

                if (WebConfigSettings.UseFoldersInsteadOfHostnamesForMultipleSites)
                {
                    string cookieName = "siteguid" + siteSettings.SiteGuid;
                    CookieHelper.SetCookie(cookieName, user.UserGuid.ToString(), persistCookie);
                }

                if (user.UserId > -1 && siteSettings.AllowUserSkins && user.Skin.Length > 0)
                {
                    SiteUtils.SetSkinCookie(user);
                }

                user.UpdateLastLoginTime();

                // track user ip address
                UserLocation userLocation = new UserLocation(user.UserGuid, SiteUtils.GetIP4Address());
                userLocation.SiteGuid = siteSettings.SiteGuid;
                userLocation.Hostname = Page.Request.UserHostName;
                userLocation.Save();

                string redirectUrl = GetRedirectPath();
                CookieHelper.ExpireCookie(returnUrlCookieName);

                UserSignInEventArgs u = new UserSignInEventArgs(user);
                OnUserSignIn(u);

                //WebUtils.SetupRedirect(this, redirectUrl);
                Response.Redirect(redirectUrl);
                return;

            }
            else
            {
                // redirect to login
                // need to make login page show
                // reason for failure
                //WebUtils.SetupRedirect(this, LoginPage);
                Response.Redirect(LoginPage);
            }
        }