/// <summary> /// Get user information and logs user (register if no user found) /// </summary> private void ProcessLiveIDLogin() { // Get authorization code from URL String code = QueryHelper.GetString("code", String.Empty); // Additional info page for login string additionalInfoPage = SettingsKeyInfoProvider.GetValue(siteName + ".CMSLiveIDRequiredUserDataPage"); // Create windows login object WindowsLiveLogin wwl = new WindowsLiveLogin(siteName); // Windows live User WindowsLiveLogin.User liveUser = null; if (!WindowsLiveLogin.UseServerSideAuthorization) { if (!RequestHelper.IsPostBack()) { // If client authentication, get token displayed in url after # from window.location String script = ControlsHelper.GetPostBackEventReference(this, "#").Replace("'#'", "window.location"); ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "PostbackScript", ScriptHelper.GetScript(script)); } else { // Try to get full url from event argument string fullurl = Request[postEventArgumentID]; // Authentication token - use to get uid String token = ParseToken(fullurl, @"authentication_token=([\w\d.-]+)&"); // User token - this token is used in server auth. scenario. It's stored in user object (for possible further use) so parse it too and store it String accessToken = ParseToken(fullurl, @"access_token=([%\w\d.-/]+)&"); if (token != String.Empty) { // Return context from session GetLoginInformation(); // Authenticate user by found token liveUser = wwl.AuthenticateClientToken(token, relativeURL, accessToken); if (liveUser != null) { // Set info to refresh to parent page ScriptHelper.RegisterWOpenerScript(Page); CreateCloseScript(""); } } } } else { GetLoginInformation(); // Process login via Live ID liveUser = wwl.ProcessLogin(code, relativeURL); } // Authorization successful if (liveUser != null) { // Find user by ID UserInfo winUser = UserInfoProvider.GetUserInfoByWindowsLiveID(liveUser.Id); string error = String.Empty; // Register new user if (winUser == null) { // Check whether additional user info page is set // No page set, user can be created/sign if (additionalInfoPage == String.Empty) { // Create new user UserInfo ui = AuthenticationHelper.AuthenticateWindowsLiveUser(liveUser.Id, siteName, true, ref error); // Remove live user object from session, won't be needed Session.Remove("windowsliveloginuser"); // If user was found or successfully created if ((ui != null) && (ui.Enabled)) { double resolvedConversionValue = ValidationHelper.GetDouble(MacroResolver.Resolve(conversionValue), 0); // Log user registration into the web analytics and track conversion if set AnalyticsHelper.TrackUserRegistration(siteName, ui, conversionName, resolvedConversionValue); MembershipActivityLogger.LogRegistration(ui.UserName, DocumentContext.CurrentDocument); SetAuthCookieAndRedirect(ui); } // User not created else { if (WindowsLiveLogin.UseServerSideAuthorization) { WindowsLiveLogin.ClearCookieAndRedirect(loginPage); } else { CreateCloseScript("clearcookieandredirect"); } } } // Required data page exists else { // Store user object in session for additional info page SessionHelper.SetValue("windowsliveloginuser", liveUser); if (WindowsLiveLogin.UseServerSideAuthorization) { // Redirect to additional info page URLHelper.Redirect(UrlResolver.ResolveUrl(additionalInfoPage)); } else { CreateCloseScript("redirectToAdditionalPage"); } } } else { UserInfo ui = AuthenticationHelper.AuthenticateWindowsLiveUser(liveUser.Id, siteName, true, ref error); // If user was found if ((ui != null) && (ui.Enabled)) { SetAuthCookieAndRedirect(ui); } } } }
/// <summary> /// Handles btnOkNew click, creates new user and joins it with liveid token. /// </summary> protected void btnOkNew_Click(object sender, EventArgs e) { if (liveUser != null) { // Validate entered values string errorMessage = new Validator().IsRegularExp(txtUserNameNew.Text, "^([a-zA-Z0-9_\\-\\.@]+)$", GetString("mem.liveid.fillcorrectusername")) .IsEmail(txtEmail.Text, GetString("mem.liveid.fillvalidemail")).Result; string password = passStrength.Text.Trim(); // 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(SiteContext.CurrentSiteName); } } string siteName = SiteContext.CurrentSiteName; // Check whether email is unique if it is required if ((errorMessage == String.Empty) && !UserInfoProvider.IsEmailUnique(txtEmail.Text.Trim(), siteName, 0)) { errorMessage = GetString("UserInfo.EmailAlreadyExist"); } // Check reserved names if ((errorMessage == String.Empty) && UserInfoProvider.NameIsReserved(siteName, txtUserNameNew.Text.Trim())) { errorMessage = GetString("Webparts_Membership_RegistrationForm.UserNameReserved").Replace("%%name%%", HTMLHelper.HTMLEncode(txtUserNameNew.Text.Trim())); } if (errorMessage == String.Empty) { string userName = txtUserNameNew.Text.Trim(); // Check if user with given username already exists UserInfo ui = UserInfoProvider.GetUserInfo(userName); UserInfo siteui = UserInfoProvider.GetUserInfo(UserInfoProvider.EnsureSitePrefixUserName(userName, SiteContext.CurrentSite)); // User with given username is already registered if ((ui != null) || (siteui != null)) { plcError.Visible = true; lblError.Text = GetString("mem.openid.usernameregistered"); } else { // Register new user string error = DisplayMessage; ui = AuthenticationHelper.AuthenticateWindowsLiveUser(liveUser.Id, siteName, false, ref error); DisplayMessage = error; if (ui != null) { // Set additional information ui.UserName = ui.UserNickName = ui.FullName = userName; // Ensure site prefixes if (UserInfoProvider.UserNameSitePrefixEnabled(siteName)) { ui.UserName = UserInfoProvider.EnsureSitePrefixUserName(userName, SiteContext.CurrentSite); } 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 Session.Remove("windowsliveloginuser"); // Send registration e-mails AuthenticationHelper.SendRegistrationEmails(ui, ApprovalPage, true, SendWelcomeEmail); // Notify administrator bool requiresConfirmation = SettingsKeyInfoProvider.GetBoolValue(siteName + ".CMSRegistrationEmailConfirmation"); if (!requiresConfirmation && NotifyAdministrator && (FromAddress != String.Empty) && (ToAddress != String.Empty)) { 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); // 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; } } }