/// <summary> /// Signs in user or registers a new user. /// </summary> private void SetupControlForPublicUser() { RegisterFacebookScript(); // 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 has signed in if (!String.IsNullOrEmpty(facebookUserId) && facebookCookiesValid) { UserInfo ui = UserInfoProvider.GetUserInfoByFacebookConnectID(facebookUserId); // Claimed Facebook ID is in DB if (ui != null) { SignInUser(ui, facebookUserId, confirmToken); } // Claimed Facebook ID not found = save new user else { RegisterNewUser(facebookUserId, confirmToken); } } }
/// <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(); } } }