/// <summary> /// Handles btnOkNew click, creates new user and joins it with openID token. /// </summary> protected void btnOkNew_Click(object sender, EventArgs e) { if (response != null) { // Validate entered values string errorMessage = new Validator().IsRegularExp(txtUserNameNew.Text, "^([a-zA-Z0-9_\\-\\.@]+)$", GetString("mem.openid.fillcorrectusername")) .IsEmail(txtEmail.Text, GetString("mem.openid.fillvalidemail")).Result; string siteName = CMSContext.CurrentSiteName; string password = passStrength.Text; // If password is enabled to set, check it if (plcPasswordNew.Visible && (errorMessage == String.Empty)) { if (password == String.Empty) { errorMessage = GetString("mem.liveid.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(), siteName, 0)) { errorMessage = GetString("UserInfo.EmailAlreadyExist"); } // Check reserved names if (string.IsNullOrEmpty(errorMessage) && UserInfoProvider.NameIsReserved(siteName, 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 { string error = DisplayMessage; // Register new user ui = AuthenticationHelper.AuthenticateOpenIDUser((string)response["ClaimedIdentifier"], ValidationHelper.GetString(SessionHelper.GetValue(SESSION_NAME_URL), null), siteName, true, false, ref error); DisplayMessage = error; // If user successfully created if (ui != null) { // Set additional information ui.UserName = ui.UserNickName = ui.FullName = txtUserNameNew.Text.Trim(); ui.Email = txtEmail.Text; // Load values submitted by OpenID provider // Load date of birth DateTime birthdate = (DateTime)response["BirthDate"]; if (birthdate != DateTime.MinValue) { ui.UserSettings.UserDateOfBirth = birthdate; } // Load default country var culture = (System.Globalization.CultureInfo)response["Culture"]; if (culture != null) { ui.PreferredCultureCode = culture.Name; } // Nick name string nick = (string)response["Nickname"]; if (!String.IsNullOrEmpty(nick)) { ui.UserSettings.UserNickName = nick; } // Full name string full = (string)response["FullName"]; if (!String.IsNullOrEmpty(full)) { ui.FullName = full; } // User gender var gender = (int?)response["UserGender"]; if (gender != null) { ui.UserSettings.UserGender = (int)gender; } // 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; } // Set user UserInfoProvider.SetUserInfo(ui); // Clear used session SessionHelper.Remove(SESSION_NAME_URL); SessionHelper.Remove(SESSION_NAME_USERDATA); AuthenticationHelper.SendRegistrationEmails(ui, ApprovalPage, password, true, SendWelcomeEmail); // Notify administrator bool requiresConfirmation = SettingsKeyProvider.GetBoolValue(siteName + ".CMSRegistrationEmailConfirmation"); if (!requiresConfirmation && NotifyAdministrator && (FromAddress != String.Empty) && (ToAddress != String.Empty)) { AuthenticationHelper.NotifyAdministrator(ui, FromAddress, ToAddress); } // Log registration into analytics AuthenticationHelper.TrackUserRegistration(TrackConversionName, ConversionValue, siteName, 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); if (!String.IsNullOrEmpty(DisplayMessage)) { lblInfo.Visible = true; lblInfo.Text = DisplayMessage; plcForm.Visible = false; } else { URLHelper.Redirect(ResolveUrl("~/Default.aspx")); } } } } // Validation failed - display error message else { lblError.Text = errorMessage; plcError.Visible = true; } } }
/// <summary> /// Checks status of current user. /// </summary> protected void CheckStatus() { // Get current site name string siteName = SiteContext.CurrentSiteName; string error = null; // Check return URL string returnUrl = QueryHelper.GetString("returnurl", null); returnUrl = HttpUtility.UrlDecode(returnUrl); // Get current URL string currentUrl = RequestContext.CurrentURL; currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "token"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.ns"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.mode"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.return_to"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.claimed_id"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.identity"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.assoc_handle"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.realm"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.response_nonce"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.signed"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.op_endpoint"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.pape.auth_level.nist"); currentUrl = URLHelper.RemoveParameterFromUrl(currentUrl, "openid.sig"); // Get OpenID response status switch (openIDhelper.CheckStatus()) { // User is authenticated case CMSOpenIDHelper.RESPONSE_AUTHENTICATED: // Claimed ID not found = save new user if (OpenIDUserInfoProvider.GetUserInfoByOpenID(openIDhelper.ClaimedIdentifier) == null) { // Check whether additional user info page is set string additionalInfoPage = SettingsKeyInfoProvider.GetValue(siteName + ".CMSRequiredOpenIDPage").Trim(); // No page set, user can be created if (String.IsNullOrEmpty(additionalInfoPage)) { // Register new user UserInfo ui = AuthenticationHelper.AuthenticateOpenIDUser(openIDhelper.ClaimedIdentifier, ValidationHelper.GetString(SessionHelper.GetValue(SESSION_NAME_URL), null), siteName, false, true, ref error); // If user was found or successfuly created if (ui != null) { // Load values submited by OpenID provider // Load date of birth if (openIDhelper.BirthDate != DateTime.MinValue) { ui.UserSettings.UserDateOfBirth = openIDhelper.BirthDate; } // Load default country if (openIDhelper.Culture != null) { ui.PreferredCultureCode = openIDhelper.Culture.Name; } // Load e-mail if (!String.IsNullOrEmpty(openIDhelper.Email)) { ui.Email = openIDhelper.Email; } // Nick name if (!String.IsNullOrEmpty(openIDhelper.Nickname)) { ui.UserSettings.UserNickName = openIDhelper.Nickname; } // User gender if (openIDhelper.UserGender != null) { ui.UserSettings.UserGender = (int)openIDhelper.UserGender; } UserInfoProvider.SetUserInfo(ui); // If user is enabled if (ui.Enabled) { // Create autentification cookie AuthenticationHelper.SetAuthCookieWithUserData(ui.UserName, true, Session.Timeout, new[] { "openidlogin" }); // Log activity MembershipActivityLogger.LogLogin(ui.UserName, DocumentContext.CurrentDocument); } // 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(siteName, ui, TrackConversionName, ConversionValue); MembershipActivityLogger.LogRegistration(ui.UserName, DocumentContext.CurrentDocument); } // Redirect when authentication was succesfull if (String.IsNullOrEmpty(error)) { if (URLHelper.IsLocalUrl(returnUrl)) { URLHelper.Redirect(returnUrl); } else { URLHelper.Redirect(currentUrl); } } // Display error otherwise else { lblError.Text = error; lblError.Visible = true; } } // Additional information page is set else { // Store user object in session for additional use StoreResponseInSession(); // Redirect to additional info page string targetURL = URLHelper.GetAbsoluteUrl(additionalInfoPage); if (URLHelper.IsLocalUrl(returnUrl)) { // Add return URL to parameter targetURL = URLHelper.AddParameterToUrl(targetURL, "returnurl", HttpUtility.UrlEncode(returnUrl)); } URLHelper.Redirect(UrlResolver.ResolveUrl(targetURL)); } } // Claimed OpenID is in DB else { // Login existing user UserInfo ui = AuthenticationHelper.AuthenticateOpenIDUser(openIDhelper.ClaimedIdentifier, ValidationHelper.GetString(SessionHelper.GetValue(SESSION_NAME_URL), null), siteName, false, true, ref error); if ((ui != null) && (ui.Enabled)) { // Create autentification cookie AuthenticationHelper.SetAuthCookieWithUserData(ui.UserName, true, Session.Timeout, new[] { "openilogin" }); // Log activity MembershipActivityLogger.LogLogin(ui.UserName, DocumentContext.CurrentDocument); // Redirect user if (URLHelper.IsLocalUrl(returnUrl)) { URLHelper.Redirect(UrlResolver.ResolveUrl(URLHelper.GetAbsoluteUrl(returnUrl))); } else { URLHelper.Redirect(currentUrl); } } // Display error which occured during authentication process else if (!String.IsNullOrEmpty(error)) { lblError.Text = error; lblError.Visible = true; } // Otherwise is user disabled else { lblError.Text = GetString("membership.userdisabled"); lblError.Visible = true; } } break; // Authentication was canceled case CMSOpenIDHelper.RESPONSE_CANCELED: lblError.Text = GetString("openid.logincanceled"); lblError.Visible = true; break; // Authentication failed case CMSOpenIDHelper.RESPONSE_FAILED: lblError.Text = GetString("openid.loginfailed"); lblError.Visible = true; break; } }