コード例 #1
0
    public Guid SaveUserData()
    {
        if (!EditMode)
        {
            Guid result = Guid.Empty;
            if (Session["CameFromSocialNetwork"] == null || IsInvite)
            {
                MembershipCreateStatus status = MembershipCreateStatus.Success;
                MembershipUser         user   = Membership.Provider.CreateUser(tfEmail.Text, tfPwd.Text, tfEmail.Text, "question", "answer", PortalSettings.IsApprovedCreateUser(), Guid.NewGuid(), out status);
                this.lblError.Text = string.Empty;

                switch (status)
                {
                case MembershipCreateStatus.DuplicateEmail:
                case MembershipCreateStatus.DuplicateUserName:
                    this.lblError.Text = Resources.Appleseed.USER_ALREADY_EXISTS;
                    break;

                case MembershipCreateStatus.ProviderError:
                    break;

                case MembershipCreateStatus.Success:
                    UpdateProfile();
                    result = (Guid)user.ProviderUserKey;
                    //if the user is registering himself (thus, is not yet authenticated) we will sign him on and send him to the home page.
                    if (!Context.User.Identity.IsAuthenticated)
                    {
                        PortalSecurity.SignOn(tfEmail.Text, tfPwd.Text, false, HttpUrlBuilder.BuildUrl());
                    }
                    break;

                // for every other error message...
                default:
                    this.lblError.Text = Resources.Appleseed.USER_SAVING_ERROR;
                    break;
                }
                return(result);
            }
            else
            {
                if ((Session["TwitterUserName"] != null) || (Session["LinkedInUserName"] != null))
                {
                    // Register Twitter or LinkedIn
                    string userName = (Session["TwitterUserName"] != null) ? Session["TwitterUserName"].ToString() : Session["LinkedInUserName"].ToString();
                    string password = GeneratePasswordHash(userName);

                    MembershipCreateStatus status = MembershipCreateStatus.Success;
                    MembershipUser         user   = Membership.Provider.CreateUser(userName, password, tfEmail.Text, "question", "answer", PortalSettings.IsApprovedCreateUser(), Guid.NewGuid(), out status);
                    this.lblError.Text = string.Empty;

                    switch (status)
                    {
                    case MembershipCreateStatus.DuplicateEmail:
                    case MembershipCreateStatus.DuplicateUserName:
                        this.lblError.Text = Resources.Appleseed.USER_ALREADY_EXISTS;
                        break;

                    case MembershipCreateStatus.ProviderError:
                        break;

                    case MembershipCreateStatus.Success:
                        UpdateProfile();
                        result = (Guid)user.ProviderUserKey;
                        //if the user is registering himself (thus, is not yet authenticated) we will sign him on and send him to the home page.
                        if (!Context.User.Identity.IsAuthenticated)
                        {
                            Session.Contents.Remove("CameFromSocialNetwork");
                            PortalSecurity.SignOn(userName, password, false, HttpUrlBuilder.BuildUrl());
                        }

                        break;

                    // for every other error message...
                    default:
                        this.lblError.Text = Resources.Appleseed.USER_SAVING_ERROR;
                        break;
                    }

                    return(result);
                }
                else if (Session["FacebookUserName"] != null || Session["GoogleUserEmail"] != null)
                {
                    // Register Facebook
                    string userName = tfEmail.Text;
                    string password = GeneratePasswordHash(userName);
                    MembershipCreateStatus status = MembershipCreateStatus.Success;
                    MembershipUser         user   = Membership.Provider.CreateUser(userName, password, userName, "question", "answer", PortalSettings.IsApprovedCreateUser(), Guid.NewGuid(), out status);
                    this.lblError.Text = string.Empty;

                    switch (status)
                    {
                    case MembershipCreateStatus.DuplicateEmail:
                    case MembershipCreateStatus.DuplicateUserName:
                        this.lblError.Text = Resources.Appleseed.USER_ALREADY_EXISTS;
                        break;

                    case MembershipCreateStatus.ProviderError:
                        break;

                    case MembershipCreateStatus.Success:
                        UpdateProfile();
                        result = (Guid)user.ProviderUserKey;
                        //if the user is registering himself (thus, is not yet authenticated) we will sign him on and send him to the home page.
                        if (!Context.User.Identity.IsAuthenticated)
                        {
                            // Removing names from social networks of sessions


                            if (Session["CameFromGoogleLogin"] != null)
                            {
                                Session.Contents.Remove("CameFromGoogleLogin");
                            }
                            if (Session["GoogleUserEmail"] != null)
                            {
                                Session.Contents.Remove("GoogleUserEmail");
                            }
                            if (Session["GoogleUserName"] != null)
                            {
                                Session.Contents.Remove("GoogleUserName");
                            }
                            if (Session["FacebookUserName"] != null)
                            {
                                Session.Contents.Remove("FacebookUserName");
                            }
                            if (Session["FacebookName"] != null)
                            {
                                Session.Contents.Remove("FacebookName");
                            }

                            PortalSecurity.SignOn(userName, password, false, HttpUrlBuilder.BuildUrl());
                        }

                        break;

                    // for every other error message...
                    default:
                        this.lblError.Text = Resources.Appleseed.USER_SAVING_ERROR;
                        break;
                    }


                    return(result);
                }
                else
                {
                    return(result);
                }
            }
        }
        else
        {
            string Email    = tfEmail.Text;
            string UserName = Membership.GetUserNameByEmail(Email);
            if (!UserName.Equals(Email))
            {
                // The user Came from twitter
                Session["CameFromSocialNetwork"] = true;
                Session["TwitterUserName"]       = UserName;
                Session["LinkedInUserName"]      = UserName;
                Session["deleteCookies"]         = true;
            }
            UpdateProfile();
            return((Guid)Membership.GetUser(UserName, false).ProviderUserKey);
        }
    }