/// <summary> /// Registers new user. /// </summary> /// <param name="facebookUserId">The user's Facebook ID</param> /// <param name="facebookAccessToken">The user's access token retrieved from Facebook</param> private void RegisterNewUser(string facebookUserId, string facebookAccessToken) { // Check whether additional user info page is set string currentSiteName = SiteContext.CurrentSiteName; // Register new user string error = null; UserInfo ui = AuthenticationHelper.AuthenticateFacebookConnectUser(facebookUserId, currentSiteName, false, true, ref error); // If user was found or successfully created if (ui != null) { MapFacebookUserProfile(FacebookUserProfileMappingTriggerEnum.Registration, ui, facebookUserId, facebookAccessToken); // Notify administrator if (NotifyAdministrator && !String.IsNullOrEmpty(FromAddress) && !String.IsNullOrEmpty(ToAddress)) { AuthenticationHelper.NotifyAdministrator(ui, FromAddress, ToAddress); } // Log user registration into the web analytics and track conversion if set AnalyticsHelper.TrackUserRegistration(currentSiteName, ui, TrackConversionName, ConversionValue); MembershipActivityLogger.LogRegistration(ui.UserName, DocumentContext.CurrentDocument); // Signs in created user and redirects her to the return URL SignInUser(ui, facebookUserId, facebookAccessToken, false); } lblError.Text = error; lblError.Visible = true; }
/// <summary> /// Registers new user. /// </summary> /// <param name="facebookUserId">The user's Facebook ID</param> /// <param name="facebookAccessToken">The user's access token retrieved from Facebook</param> private void RegisterNewUser(string facebookUserId, string facebookAccessToken) { // Check whether additional user info page is set string currentSiteName = SiteContext.CurrentSiteName; // Register new user string error = null; UserInfo ui = AuthenticationHelper.AuthenticateFacebookConnectUser(facebookUserId, currentSiteName, false, true, ref error); // If user was found or successfuly created if (ui != null) { // Send registration e-mails // E-mail confirmation is not required as user already provided confirmation by successful login using Facebook connect AuthenticationHelper.SendRegistrationEmails(ui, null, null, false, false); // Notify administrator if (NotifyAdministrator && !String.IsNullOrEmpty(FromAddress) && !String.IsNullOrEmpty(ToAddress)) { AuthenticationHelper.NotifyAdministrator(ui, FromAddress, ToAddress); } // Log user registration into the web analytics and track conversion if set AnalyticsHelper.TrackUserRegistration(currentSiteName, ui, TrackConversionName, ConversionValue); Activity activity = new ActivityRegistration(ui, DocumentContext.CurrentDocument, AnalyticsContext.ActivityEnvironmentVariables); if (activity.Data != null) { activity.Data.ContactID = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui); activity.Log(); } MapFacebookUserProfile(FacebookUserProfileMappingTriggerEnum.Registration, ui, facebookUserId, facebookAccessToken); // Signs in created user and redirects her to the return URL SignInUser(ui, facebookUserId, facebookAccessToken); } lblError.Text = error; lblError.Visible = true; }
/// <summary> /// Initializes the control properties. /// </summary> protected void SetupControl() { if (StopProcessing) { Visible = false; } else { if (QueryHelper.GetInteger("logout", 0) > 0) { // Sign out from CMS CMSContext.LogoutUser(); CMSContext.CurrentUser = null; Response.Cache.SetNoStore(); URLHelper.Redirect(URLHelper.RemoveParameterFromUrl(URLHelper.CurrentURL, "logout")); return; } string currentSiteName = CMSContext.CurrentSiteName; if (!String.IsNullOrEmpty(currentSiteName) && SettingsKeyProvider.GetBoolValue(currentSiteName + ".CMSEnableFacebookConnect")) { // Check Facebook Connect settings if (!FacebookConnectHelper.FacebookIsAvailable(currentSiteName)) { // Display warning message in "Design mode" if (DisplayMessage()) { return; } Visible = false; return; } // Try to retrieve return URL from query string returnUrl = QueryHelper.GetString("returnurl", null); // Init Facebook Connect if (Page is ContentPage) { // Adding XML namespace ((ContentPage)Page).XmlNamespace = FacebookConnectHelper.GetFacebookXmlNamespace(); } // Init FB connect string initscr = FacebookConnectHelper.GetFacebookInitScriptForSite(currentSiteName) + " " + FacebookConnectHelper.GetFacebookLoginHandlerScript(); ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "FBConnectLogon" + ClientID, initscr); // Return URL string currentUrl = URLHelper.AddParameterToUrl(URLHelper.CurrentURL, "logout", "1"); string additionalScript = "window.location.href=" + ScriptHelper.GetString(URLHelper.GetAbsoluteUrl(currentUrl)) + "; return false;"; // Logout script for FB connect string logoutScript = FacebookConnectHelper.GetFacebookLogoutScriptForSignOut(URLHelper.CurrentURL, FacebookConnectHelper.GetFacebookApiKey(currentSiteName), additionalScript); // Validate FB access token against FB server string facebookUserId = null; bool facebookCookiesValid = false; string confirmToken = QueryHelper.GetString(CONFIRMATION_URLPARAMETER, null); if (!String.IsNullOrEmpty(confirmToken)) { facebookCookiesValid = FacebookConnectHelper.ValidateFBAccessToken(confirmToken, out facebookUserId); } // If user is already authenticated if (CMSContext.CurrentUser.IsAuthenticated()) { // Is user logged in using Facebook Connect? if ((CMSContext.CurrentUser.UserSettings != null) && String.IsNullOrEmpty(CMSContext.CurrentUser.UserSettings.UserFacebookID)) { // no, user is not logged in by Facebook Connect logoutScript = additionalScript; } // Hide Facebook Connect button plcFBButton.Visible = false; // If signout should be visible and user has FacebookID registered if (ShowSignOut && !String.IsNullOrEmpty(CMSContext.CurrentUser.UserSettings.UserFacebookID)) { // If only text is set use text/button link if (!String.IsNullOrEmpty(SignOutText)) { // Button link if (ShowAsButton) { btnSignOut.OnClientClick = logoutScript; btnSignOut.Text = SignOutText; btnSignOut.Visible = true; } // Text link else { lnkSignOutLink.Text = SignOutText; lnkSignOutLink.Visible = true; lnkSignOutLink.Attributes.Add("onclick", logoutScript); lnkSignOutLink.Attributes.Add("style", "cursor:pointer;"); } } // Image link else { string signOutImageUrl = SignOutImageURL; // Use default image if none is specified if (String.IsNullOrEmpty(signOutImageUrl)) { signOutImageUrl = GetImageUrl("Others/FacebookConnect/signout.gif"); } imgSignOut.ImageUrl = ResolveUrl(signOutImageUrl); imgSignOut.Visible = true; imgSignOut.AlternateText = GetString("webparts_membership_signoutbutton.signout"); lnkSignOutImageBtn.Visible = true; lnkSignOutImageBtn.Attributes.Add("onclick", logoutScript); lnkSignOutImageBtn.Attributes.Add("style", "cursor:pointer;"); } } else { Visible = false; } } // Sign In else { if (facebookCookiesValid) { if (!String.IsNullOrEmpty(facebookUserId)) { UserInfo ui = UserInfoProvider.GetUserInfoByFacebookConnectID(facebookUserId); // Claimed Facebook ID is in DB if (ui != null) { // Login existing user if ((ui != null) && ui.Enabled) { // Ban IP addresses which are blocked for login BannedIPInfoProvider.CheckIPandRedirect(currentSiteName, BanControlEnum.Login); // Create autentification cookie AuthenticationHelper.SetAuthCookieWithUserData(ui.UserName, true, Session.Timeout, new string[] { "facebooklogon" }); UserInfoProvider.SetPreferredCultures(ui); int contactId = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui); Activity activity = new ActivityUserLogin(contactId, ui, CMSContext.CurrentDocument, CMSContext.ActivityEnvironmentVariables); activity.Log(); // Redirect user if (String.IsNullOrEmpty(returnUrl)) { returnUrl = URLHelper.RemoveParameterFromUrl(URLHelper.CurrentURL, CONFIRMATION_URLPARAMETER); } URLHelper.Redirect(returnUrl); } // Otherwise is user disabled else { lblError.Text = GetString("membership.userdisabled"); lblError.Visible = true; } } // Claimed Facebook ID not found = save new user else { // Check whether additional user info page is set string additionalInfoPage = SettingsKeyProvider.GetStringValue(currentSiteName + ".CMSRequiredFacebookPage").Trim(); // No page set, user can be created if (String.IsNullOrEmpty(additionalInfoPage)) { // Register new user string error = null; ui = AuthenticationHelper.AuthenticateFacebookConnectUser(facebookUserId, currentSiteName, false, true, ref error); // If user was found or successfuly created if (ui != null) { // If user is enabled if (ui.Enabled) { // Create authentification cookie AuthenticationHelper.SetAuthCookieWithUserData(ui.UserName, true, Session.Timeout, new string[] { "facebooklogon" }); int contactID = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui); Activity activityLogin = new ActivityUserLogin(contactID, ui, CMSContext.CurrentDocument, CMSContext.ActivityEnvironmentVariables); activityLogin.Log(); } // Send registration e-mails // E-mail confirmation is not required as user already provided confirmation by successful login using Facebook connect AuthenticationHelper.SendRegistrationEmails(ui, null, null, false, false); // Notify administrator if (NotifyAdministrator && !String.IsNullOrEmpty(FromAddress) && !String.IsNullOrEmpty(ToAddress)) { AuthenticationHelper.NotifyAdministrator(ui, FromAddress, ToAddress); } // Log registration into analytics AuthenticationHelper.TrackUserRegistration(TrackConversionName, ConversionValue, currentSiteName, ui); Activity activity = new ActivityRegistration(ui, CMSContext.CurrentDocument, CMSContext.ActivityEnvironmentVariables); if (activity.Data != null) { activity.Data.ContactID = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui); activity.Log(); } } // Redirect when authentication was succesfull if (String.IsNullOrEmpty(error)) { if (!String.IsNullOrEmpty(returnUrl)) { URLHelper.Redirect(URLHelper.GetAbsoluteUrl(returnUrl)); } else { URLHelper.Redirect(URLHelper.RemoveParameterFromUrl(URLHelper.CurrentURL, CONFIRMATION_URLPARAMETER)); } } // Display error otherwise else { lblError.Text = error; lblError.Visible = true; } } // Additional information page is set else { // Store user object in session for additional info page SessionHelper.SetValue(SESSION_NAME_USERDATA, facebookUserId); // Redirect to additional info page string targetURL = URLHelper.GetAbsoluteUrl(additionalInfoPage); if (!String.IsNullOrEmpty(returnUrl)) { // Add return URL to parameter targetURL = URLHelper.AddParameterToUrl(targetURL, "returnurl", HttpUtility.UrlEncode(returnUrl)); } URLHelper.Redirect(targetURL); } } } } } } else { // Show warning message in "Design mode" Visible = DisplayMessage(); } } }
/// <summary> /// Handles btnOkNew click, creates new user and joins it with liveid token. /// </summary> protected void btnOkNew_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(facebookUserId)) { string password = passStrength.Text; string currentSiteName = CMSContext.CurrentSiteName; // Validate entered values string errorMessage = new Validator().IsRegularExp(txtUserNameNew.Text, "^([a-zA-Z0-9_\\-\\.@]+)$", GetString("mem.facebook.fillcorrectusername")) .IsEmail(txtEmail.Text, GetString("mem.facebook.fillvalidemail")).Result; // If password is enabled to set, check it if (plcPasswordNew.Visible && (String.IsNullOrEmpty(errorMessage))) { if (String.IsNullOrEmpty(password)) { errorMessage = GetString("mem.facebook.specifyyourpass"); } else if (password != txtConfirmPassword.Text.Trim()) { errorMessage = GetString("webparts_membership_registrationform.passwordonotmatch"); } // Check policy if (!passStrength.IsValid()) { errorMessage = AuthenticationHelper.GetPolicyViolationMessage(CMSContext.CurrentSiteName); } } // Check whether email is unique if it is required if ((String.IsNullOrEmpty(errorMessage)) && !UserInfoProvider.IsEmailUnique(txtEmail.Text.Trim(), currentSiteName, 0)) { errorMessage = GetString("UserInfo.EmailAlreadyExist"); } // Check reserved names if ((String.IsNullOrEmpty(errorMessage)) && UserInfoProvider.NameIsReserved(currentSiteName, txtUserNameNew.Text.Trim())) { errorMessage = GetString("Webparts_Membership_RegistrationForm.UserNameReserved").Replace("%%name%%", HTMLHelper.HTMLEncode(txtUserNameNew.Text.Trim())); } if (String.IsNullOrEmpty(errorMessage)) { // Check if user with given username already exists UserInfo ui = UserInfoProvider.GetUserInfo(txtUserNameNew.Text.Trim()); // User with given username is already registered if (ui != null) { plcError.Visible = true; lblError.Text = GetString("mem.openid.usernameregistered"); } else { // Register new user string error = DisplayMessage; ui = AuthenticationHelper.AuthenticateFacebookConnectUser(facebookUserId, currentSiteName, true, false, ref error); DisplayMessage = error; if (ui != null) { // Set additional information ui.UserName = ui.UserNickName = txtUserNameNew.Text.Trim(); ui.Email = txtEmail.Text; // Set password if (plcPasswordNew.Visible) { UserInfoProvider.SetPassword(ui, password); // If user can choose password then is not considered external(external user can't login in common way) ui.IsExternal = false; } UserInfoProvider.SetUserInfo(ui); // Remove live user object from session, won't be needed SessionHelper.Remove(SESSION_NAME_USERDATA); // Send registration e-mails AuthenticationHelper.SendRegistrationEmails(ui, ApprovalPage, password, true, SendWelcomeEmail); // Notify administrator bool requiresConfirmation = SettingsKeyProvider.GetBoolValue(currentSiteName + ".CMSRegistrationEmailConfirmation"); if (!requiresConfirmation && NotifyAdministrator && (FromAddress != String.Empty) && (ToAddress != String.Empty)) { AuthenticationHelper.NotifyAdministrator(ui, FromAddress, ToAddress); } // Log registration into analytics AuthenticationHelper.TrackUserRegistration(TrackConversionName, ConversionValue, currentSiteName, ui); Activity activity = new ActivityRegistration(ui, CMSContext.CurrentDocument, CMSContext.ActivityEnvironmentVariables); if (activity.Data != null) { activity.Data.ContactID = ModuleCommands.OnlineMarketingGetUserLoginContactID(ui); activity.Log(); } // Set authentication cookie and redirect to page SetAuthCookieAndRedirect(ui); // Display error message if (!String.IsNullOrEmpty(DisplayMessage)) { lblInfo.Visible = true; lblInfo.Text = DisplayMessage; plcForm.Visible = false; } else { URLHelper.Redirect(ResolveUrl("~/Default.aspx")); } } } } else { lblError.Text = errorMessage; plcError.Visible = true; } } }