Beispiel #1
0
        protected override void OnCreatedUser(EventArgs e)
        {
            // Note: this doesn't run using the privileges of the anonymous user, so we elevate them
            // Also, you can't use the original Site even with elevated privileges, otherwise it reverts back to anonymous.
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site2 = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.Zone))
                {
                    using (SPWeb web2 = site2.OpenWeb(SPContext.Current.Web.ID))
                    {
                        // from this point allowunsafeupdates is required because the call is initiated from a browser with
                        // anonymouse rights only
                        web2.AllowUnsafeUpdates = true;

                        MembershipRequest request = new MembershipRequest();
                        request.UserEmail         = this.Email;
                        request.UserName          = this.UserName;
                        if (System.Web.Security.Membership.RequiresQuestionAndAnswer)
                        {
                            request.PasswordQuestion = this.Question;
                            request.PasswordAnswer   = this.Answer;
                        }
                        request.FirstName    = this.FirstName;
                        request.LastName     = this.LastName;
                        request.DefaultGroup = this._DefaultGroup;

                        request.SiteName = web2.Title;
                        request.SiteURL  = web2.Url;

                        MembershipSettings settings = new MembershipSettings(web2);

                        if (settings.ReviewMembershipRequests)
                        {
                            request.LoginCreatedUser = false;

                            if (!MembershipRequest.CopyToReviewList(request))
                            {
                                lblError.Text = this.UnknownErrorMessage;
                                return;
                            }
                        }
                        else
                        {
                            #region Process new user request if we're NOT using the Request List

                            if (!AutoGeneratePassword)
                            {
                                request.Password = this.Password;
                            }

                            request.ChangePasswordURL = Utils.GetAbsoluteURL(web2, settings.ChangePasswordPage);
                            request.LoginCreatedUser  = SPLoginCreatedUser;

                            try
                            {
                                MembershipRequest.ApproveMembership(request, web2);
                            }
                            catch (Exception ex)
                            {
                                Utils.LogError(ex);
                                this.lblCompleteSuccess.Text = this.UnknownErrorMessage;
                                return;
                            }


                            #endregion
                        }
                        this.MoveTo(this.CompleteStep);
                    }
                }
            });
        }