コード例 #1
0
        public static MembershipUserWrapper CreateFromRegistration(Registration registration)
        {
            Boolean exists = (System.Web.Security.Membership.GetUser(registration.Email) != null);

            MembershipUserWrapper wrapper = new MembershipUserWrapper();
            if (String.IsNullOrEmpty(registration.ExistingAccountGuid))
            {
                UserInfo info = new UserInfo();
                MembershipUser user = null;

                info.Username = registration.Email;
                info.Email = registration.Email;
                info.Firstname = registration.Firstname;
                info.Lastname = registration.Lastname;
                info.Company = registration.Company;
                info.Address1 = registration.Address1;
                info.Address2 = registration.Address2;
                info.City = registration.City;
                info.State = registration.State;
                info.Zipcode = registration.Zipcode;
                info.Guid = System.Guid.NewGuid().ToString();
                info.Created = UtcDateTime.Now;

                UserInfoDao dao = new UserInfoDao();
                if (!exists)
                {
                    using (Transaction tx = new Transaction())
                    {
                        dao.SaveObject(info);
                        tx.Commit();
                    }

                }

                //Create the account in the asp.net membership system
                String password = Decrypt(registration.EncryptedPassword);
                try
                {
                    if (!exists)
                        user = System.Web.Security.Membership.CreateUser(registration.Email, password, registration.Email);

                    if (!System.Web.Security.Roles.IsUserInRole(registration.Email, SecurityConstants.Roles.SITE_ADMINISTRATOR))
                        System.Web.Security.Roles.AddUserToRole(registration.Email, SecurityConstants.Roles.SITE_ADMINISTRATOR);
                }
                catch (MembershipCreateUserException e)
                {
                    //Rollback the user info
                    using (Transaction tx = new Transaction())
                    {
                        dao.DeleteObject(info);
                        tx.Commit();
                    }
                    throw e;
                }

                wrapper.MembershipUser = user;
                wrapper.UserInfo = info;
            }
            else
            {
                UserInfo info = new UserInfoDao().FindByGuid(registration.ExistingAccountGuid);
                if (info != null)
                {
                    //make sure the email addresses match
                    if (info.Email.EqualsCaseInsensitive(registration.Email))
                        wrapper = FindByUsername(info.Email);
                }
            }

            return wrapper;
        }