Exemple #1
0
        //private static BizConnectContext ctx = new BizConnectContext();
        public static bool Register(RegisterModel model, UserType userType, Company company, bool login, BizConnectContext ctx)
        {
            //if (ModelState.IsValid)
            //{
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                var membershipUser = Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    if (userType == UserType.Owner)
                        Roles.AddUserToRoles(model.UserName, new[] { "Owner", "Admin" });
                    else if (userType == UserType.Admin)
                        Roles.AddUserToRole(model.UserName, "Admin");

                    var user = ctx.Users.Find(membershipUser.ProviderUserKey);
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    ctx.Employees.Add(new Employee {Account = user, Companies = new Collection<Company>{company}});
                    if (!ctx.GetValidationErrors().Any())
                    {
                        ctx.SaveChanges();
                        if (login) FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                        return true;
                        //return RedirectToAction("Index", "Home");

                    }
                    else
                        Membership.DeleteUser(model.UserName);
                }

                //ModelState.AddModelError("", ErrorCodeToString(createStatus));
            //}

            // If we got this far, something failed, redisplay form
            //return View(model);
            return false;
        }
Exemple #2
0
        public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName)
        {
            MembershipCreateStatus CreateStatus;
            System.Web.Security.Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);

            if (CreateStatus == MembershipCreateStatus.Success)
            {
                using (BizConnectContext Context = new BizConnectContext())
                {
                    User User = Context.Users.FirstOrDefault(Usr => Usr.Username == Username);
                    User.FirstName = FirstName;
                    User.LastName = LastName;
                    Context.SaveChanges();
                }

                if (IsApproved)
                {
                    FormsAuthentication.SetAuthCookie(Username, false);
                }
            }

            return CreateStatus;
        }