Example #1
0
 private void ProcessSkinCookie()
 {
     //toggle
     if (SiteUtils.IsMobileDevice())
     {
         if (CookieHelper.CookieExists(SiteUtils.MobileUseFullViewCookieName))
         {
             CookieHelper.ExpireCookie(SiteUtils.MobileUseFullViewCookieName);
         }
         else
         {
             CookieHelper.SetCookie(SiteUtils.MobileUseFullViewCookieName, "y");
         }
     }
     else
     {
         if (CookieHelper.CookieExists(SiteUtils.NonMobileUseMobileViewCookieName))
         {
             CookieHelper.ExpireCookie(SiteUtils.NonMobileUseMobileViewCookieName);
         }
         else
         {
             CookieHelper.SetCookie(SiteUtils.NonMobileUseMobileViewCookieName, "y");
         }
     }
 }
Example #2
0
        private void DoLogout()
        {
            SiteSettings siteSettings      = CacheHelper.GetCurrentSiteSettings();
            string       winliveCookieName = "winliveid"
                                             + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture);

            string roleCookieName = SiteUtils.GetRoleCookieName(siteSettings);

            HttpCookie roleCookie = new HttpCookie(roleCookieName, string.Empty);

            roleCookie.Expires = DateTime.Now.AddMinutes(1);
            roleCookie.Path    = "/";
            Response.Cookies.Add(roleCookie);

            HttpCookie displayNameCookie = new HttpCookie("DisplayName", string.Empty);

            displayNameCookie.Expires = DateTime.Now.AddMinutes(1);
            displayNameCookie.Path    = "/";
            Response.Cookies.Add(displayNameCookie);

            // apparently we need this here for folder sites using windows auth
            //https://www.mojoportal.com/Forums/EditPost.aspx?thread=13195&forumid=2&mid=34&pageid=5&pagenumber=1
            CookieHelper.ExpireCookie("siteguid" + siteSettings.SiteGuid);

            bool useFolderForSiteDetection = ConfigHelper.GetBoolProperty("UseFoldersInsteadOfHostnamesForMultipleSites", false);

            if ((useFolderForSiteDetection) && (!WebConfigSettings.UseRelatedSiteMode))
            {
                string cookieName = "siteguid" + siteSettings.SiteGuid.ToString();

                HttpCookie siteCookie = new HttpCookie(cookieName, string.Empty);
                siteCookie.Expires = DateTime.Now.AddMinutes(1);
                siteCookie.Path    = "/";
                Response.Cookies.Add(siteCookie);

                CookieHelper.ExpireCookie("siteguid" + siteSettings.SiteGuid);
            }
            else
            {
                FormsAuthentication.SignOut();
            }

            string winLiveToken = CookieHelper.GetCookieValue(winliveCookieName);

            WindowsLiveLogin.User liveUser = null;
            if (winLiveToken.Length > 0)
            {
                WindowsLiveLogin windowsLive = WindowsLiveHelper.GetWindowsLiveLogin();

                try
                {
                    liveUser = windowsLive.ProcessToken(winLiveToken);
                    if (liveUser != null)
                    {
                        Response.Redirect(windowsLive.GetLogoutUrl());
                        Response.End();
                    }
                }
                catch (InvalidOperationException)
                {
                }
            }

            try
            {
                if (Session != null)
                {
                    Session.Clear();
                    Session.Abandon();
                }
            }
            catch (HttpException) { }

            string redirectUrl = SiteUtils.GetNavigationSiteRoot() + "/Default.aspx";

            if (!siteSettings.UseSslOnAllPages)
            {
                if (redirectUrl.StartsWith("https:"))
                {
                    redirectUrl = redirectUrl.Replace("https:", "http:");
                }
            }

            WebUtils.SetupRedirect(this, redirectUrl);
        }
Example #3
0
        private void SignInUser(SiteUser user, bool isNewUser)
        {
            if (
                (siteSettings.UseSecureRegistration) &&
                (user.RegisterConfirmGuid != Guid.Empty)
                )
            {
                Notification.SendRegistrationConfirmationLink(
                    SiteUtils.GetSmtpSettings(),
                    ResourceHelper.GetMessageTemplate("RegisterConfirmEmailMessage.config"),
                    siteSettings.DefaultEmailFromAddress,
                    siteSettings.DefaultFromEmailAlias,
                    user.Email,
                    siteSettings.SiteName,
                    SiteRoot + "/ConfirmRegistration.aspx?ticket=" +
                    user.RegisterConfirmGuid.ToString());


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

                lblError.Text = Resource.RegistrationRequiresEmailConfirmationMessage;
                litInfoNeededMessage.Visible         = false;
                pnlRequiredProfileProperties.Visible = false;
                btnCreateUser.Visible = false;

                return;
            }

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

                lblError.Text = Resource.LoginAccountLockedMessage;

                return;
            }

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

                lblError.Text = Resource.LoginNotApprovedMessage;

                return;
            }


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

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

            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 = Request.UserHostName;
            userLocation.Save();

            UserSignInEventArgs u = new UserSignInEventArgs(user);

            OnUserSignIn(u);

            if (CookieHelper.CookieExists(returnUrlCookieName))
            {
                returnUrl = CookieHelper.GetCookieValue(returnUrlCookieName);
                CookieHelper.ExpireCookie(returnUrlCookieName);
            }
            string requestedReturnUrl = SiteUtils.GetReturnUrlParam(Page, SiteRoot);

            returnUrl = requestedReturnUrl;

            if (isNewUser)
            {
                if (WebConfigSettings.PageToRedirectToAfterRegistration.Length > 0)
                {
                    returnUrl = SiteRoot + WebConfigSettings.PageToRedirectToAfterRegistration;
                }
            }

            if (String.IsNullOrEmpty(returnUrl) ||
                returnUrl.Contains("AccessDenied") ||
                returnUrl.Contains("Login") ||
                returnUrl.Contains("SignIn") ||
                returnUrl.Contains("ConfirmRegistration.aspx") ||
                returnUrl.Contains("OpenIdRpxHandler.aspx") ||
                returnUrl.Contains("RecoverPassword.aspx") ||
                returnUrl.Contains("Register")
                )
            {
                returnUrl = SiteRoot;
            }

            if (returnUrl.Length > 0)
            {
                if (SiteUtils.IsSecureRequest())
                {
                    if (returnUrl.StartsWith("http:"))
                    {
                        returnUrl = returnUrl.Replace("http:", "https:");
                    }
                }

                WebUtils.SetupRedirect(this, returnUrl);
                return;
            }

            if (SiteUtils.IsSecureRequest())
            {
                if (SiteRoot.StartsWith("http:"))
                {
                    WebUtils.SetupRedirect(this, SiteRoot.Replace("http:", "https:"));
                    return;
                }
            }


            WebUtils.SetupRedirect(this, SiteRoot);
            return;
        }
        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 DoValidation()
        {
            string action = Request.QueryString.Get("action");

            if (WebConfigSettings.DebugWindowsLive)
            {
                log.Info("action was " + action);
            }

            /*
             * If action is 'logout', clear the login cookie and redirect
             * to the logout page.
             *
             * If action is 'clearcookie', clear the login cookie and
             * return a GIF as response to signify success.
             *
             * By default, try to process a login. If login was
             * successful, cache the user token in a cookie and redirect
             * If login failed, clear the cookie and redirect
             */

            if (action == "logout")
            {
                CookieHelper.ExpireCookie(winliveCookieName);
                WebUtils.SetupRedirect(this, siteRoot + "/Logoff.aspx");

                return;
            }
            else if (action == "delauth")
            {
                HandleConsent();
                return;
            }
            else if (action == "clearcookie")
            {
                CookieHelper.ExpireCookie(winliveCookieName);

                string type;
                byte[] content;
                windowsLive.GetClearCookieResponse(out type, out content);
                Response.ContentType = type;
                Response.OutputStream.Write(content, 0, content.Length);
                Response.End();
            }
            else
            {
                // action is login
                user = windowsLive.ProcessLogin(Request.Form);
                Guid userGuid = Guid.Empty;
                if (user != null)
                {
                    // auth succeeded see if its a current mojo user or not
                    // emtpy guid means new user
                    userGuid = SiteUser.GetUserGuidFromWindowsLiveId(
                        siteSettings.SiteId,
                        user.Id);
                    persistCookie = user.UsePersistentCookie;

                    CookieHelper.SetCookie(winliveCookieName,
                                           user.Token,
                                           user.UsePersistentCookie);
                }
                else
                {
                    // auth failed so clear the cookie
                    CookieHelper.ExpireCookie(winliveCookieName);
                }

                if (user != null)
                {
                    if (userGuid == Guid.Empty)
                    {
                        // WindowsLiveID Authentication succeeded
                        // no mojo user found so send to Register
                        //WebUtils.SetupRedirect(this, "RegisterWithWindowsLiveID.aspx");

                        if (siteSettings.AllowNewRegistration)
                        {
                            WebUtils.SetupRedirect(this, siteRoot + "/Secure/RegisterWithWindowsLiveID.aspx");
                            return;
                        }


                        WebUtils.SetupRedirect(this, siteRoot);

                        return;
                    }
                    else
                    {
                        // TODO: use return url cookie if normal login
                        //WebUtils.SetupRedirect(
                        //    this,
                        //    SiteUtils.GetNavigationSiteRoot());
                        DoExistingUserLogic(userGuid);

                        return;
                    }
                }
                else
                {
                    // WindowsLiveID Authentication failed
                    //WebUtils.SetupRedirect(
                    //    this,
                    //    LoginPage);
                    Response.Redirect(LoginPage);

                    return;
                }
            }
        }
        private void DoNewUserLogic(OpenIdEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            ClaimsResponse claim = e.Response.GetExtension <ClaimsResponse>();

            if (claim == null)
            {
                return;
            }

            if (IsValidForUserCreation(e, claim))
            {
                if (SiteUser.EmailExistsInDB(siteSettings.SiteId, claim.Email))
                {
                    // show message that user should login and associate
                    // their open id account on their profile page.
                    lblError.Text = Resource.OpenIDRegisterUserEmailExistsMessage;
                    return;
                }
                else
                {
                    // create user automagically since we have all
                    // the needed data
                    SiteUser newUser = new SiteUser(siteSettings);
                    newUser.Email = claim.Email;
                    newUser.Name  = claim.FullName;
                    string loginName = newUser.Name.Replace(" ", ".").ToLower();
                    if (loginName.Length > 50)
                    {
                        loginName = loginName.Substring(0, 50);
                    }

                    if (SiteUser.LoginExistsInDB(
                            siteSettings.SiteId, loginName))
                    {
                        loginName = e.ClaimedIdentifier.ToString().Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("/", string.Empty);
                        if (loginName.Length > 50)
                        {
                            loginName = loginName.Substring(0, 50);
                        }

                        int i = 1;
                        while (SiteUser.LoginExistsInDB(
                                   siteSettings.SiteId, loginName))
                        {
                            loginName += i.ToString();
                            if (loginName.Length > 50)
                            {
                                loginName = loginName.Remove(40, 1);
                            }
                            i++;
                        }
                    }

                    newUser.LoginName        = loginName;
                    newUser.Password         = SiteUser.CreateRandomPassword(7, WebConfigSettings.PasswordGeneratorChars);
                    newUser.PasswordQuestion = Resource.ManageUsersDefaultSecurityQuestion;
                    newUser.PasswordAnswer   = Resource.ManageUsersDefaultSecurityAnswer;
                    newUser.OpenIdUri        = e.ClaimedIdentifier.ToString();
                    newUser.Save();
                    if (siteSettings.UseSecureRegistration)
                    {
                        newUser.SetRegistrationConfirmationGuid(Guid.NewGuid());
                    }


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

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

                        lblError.Text = Resource.LoginUnconfirmedEmailMessage;
                        log.Info("Automatically created User " + newUser.Name + " on login from open id. Tried to login but email address is not confirmed.");

                        return;
                    }

                    if (siteSettings.UseEmailForLogin)
                    {
                        FormsAuthentication.SetAuthCookie(
                            newUser.Email, true);
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(
                            newUser.LoginName, true);
                    }

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

                    newUser.UpdateLastLoginTime();

                    string redirectUrl = GetRedirectPath();
                    CookieHelper.ExpireCookie(returnUrlCookieName);
                    WebUtils.SetupRedirect(this, redirectUrl);
                    return;
                }
            }
            else
            {
                // user not found
                // required fields not available from open id
                // redirect to register page?
                // Or show message with Link to
                // register page
                string registerLinkHref = siteRoot
                                          + "/Secure/RegisterWithOpenID.aspx";

                litNotRegisteredYetMessage.Text
                    = string.Format(
                          Resource.OpenIDMustRegisterBeforeLoginMesage,
                          registerLinkHref);
            }
        }
        private void DoExistingUserLogic(Guid userGuid)
        {
            // user found so login if allowed
            SiteUser user = new SiteUser(siteSettings, userGuid);

            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());

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

                return;
            }

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

                return;
            }


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

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

            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);
            return;
        }