예제 #1
0
        public async Task <ActionResult> RegisterVolunteerUser(RegisterVolunteerViewModel model)
        {
            if (ModelState.IsValid)
            {
                LogicLayer.VolunteerManager volMgr = new LogicLayer.VolunteerManager();
                try
                {
                    if (volMgr.FindVolunteer(model.Email))
                    {
                        return(RedirectToAction("Register", "Account"));
                    }
                    else
                    {
                        var volunteer = new DataTransferObjects.Volunteer
                        {
                            FirstName   = model.GivenName,
                            LastName    = model.FamilyName,
                            Email       = model.Email,
                            PhoneNumber = model.PhoneNumber
                        };
                        if (volMgr.AddVolunteer(volunteer))
                        {
                            var VolunteerID = volMgr.RetrieveVolunteerIDFromEmail(model.Email);
                            var user        = new ApplicationUser
                            {
                                VolEmail   = model.Email,
                                GivenName  = model.GivenName,
                                FamilyName = model.FamilyName,
                                UserName   = model.Email,
                                Email      = model.Email
                            };
                            var result = await UserManager.CreateAsync(user, "newuser");

                            if (result.Succeeded)
                            {
                                return(RedirectToAction("Index", "Admin"));
                            }
                            AddErrors(result);
                        }
                    }
                }
                catch (Exception)
                {
                    return(View(model));
                }
            }
            return(View(model));
        }
예제 #2
0
        public async Task <ActionResult> RegisterVolunteer(RegisterVolunteerViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var user = new Volunteer
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    Description = model.Description,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    Age         = model.Age,
                    CreatedOn   = DateTime.UtcNow,
                    Image       = model.Image
                };
                var result = await this.UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await this.SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    if (!this.RoleManager.RoleExists(RoleConstants.VolunteerRoleConstant))
                    {
                        var role = new IdentityRole {
                            Name = RoleConstants.VolunteerRoleConstant
                        };
                        this.RoleManager.Create(role);
                    }

                    await this.UserManager.AddToRoleAsync(user.Id, RoleConstants.VolunteerRoleConstant);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    return(this.RedirectToAction("Index", "Home"));
                }

                this.AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(this.View("Register", model));
        }