예제 #1
0
        public async Task <ActionResult> Create(RegisterViewModel userViewModel, HttpPostedFileBase avatarImage, params string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = userViewModel.Email, Email = userViewModel.Email
                };
                var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);

                //Add User to the selected Roles
                if (adminresult.Succeeded)
                {
                    if (selectedRoles != null)
                    {
                        var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles);

                        if (!result.Succeeded)
                        {
                            ModelState.AddModelError("", result.Errors.First());
                            ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
                            return(View());
                        }
                    }

                    #region Dealing with custom user details (added by jin)
                    UserDetail newUserDeets = new UserDetail();
                    newUserDeets.UserID    = user.Id;
                    newUserDeets.FirstName = userViewModel.FirstName;
                    newUserDeets.LastName  = userViewModel.LastName;

                    // handle file upload
                    string image = "noImage.jpg";
                    if (avatarImage != null)
                    {
                        image = avatarImage.FileName;
                        string   ext      = image.Substring(image.LastIndexOf("."));
                        string[] goodExts = new string[] { ".jpg", ".jpeg", ".gif", ".png" };

                        if (goodExts.Contains(ext.ToLower()))
                        {
                            image = Guid.NewGuid() + ext;
                            //avatarImage.SaveAs(Server.MapPath("~/Content/Images/Users/" + image));
                            //after resizing the above saving is not needed.

                            //resizing an image file.

                            string savePath       = Server.MapPath("~/Content/Images/Users/");
                            Image  convertedImage = Image.FromStream(avatarImage.InputStream);
                            ImageUtilities.ResizeImage(savePath, image, convertedImage, 500, 100);
                        }
                        else
                        {
                            image = "noImage.jpg";
                        }
                    }
                    newUserDeets.AvatarImage = image;
                    //end image upload

                    FamilySiteEntities db = new FamilySiteEntities();
                    db.UserDetails.Add(newUserDeets);
                    db.SaveChanges();
                    #endregion
                }
                else
                {
                    ModelState.AddModelError("", adminresult.Errors.First());
                    ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                    return(View());
                }
                return(RedirectToAction("Index"));
            }
            ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
            return(View());
        }
예제 #2
0
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase avatarImage)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "User"); //added by jin to make default role as User when a person registers.

                    #region Dealing with custom user details (added by jin)
                    UserDetail newUserDeets = new UserDetail();
                    newUserDeets.UserID    = user.Id;
                    newUserDeets.FirstName = model.FirstName;
                    newUserDeets.LastName  = model.LastName;

                    // handle file upload
                    string image = "noImage.jpg";
                    if (avatarImage != null)
                    {
                        image = avatarImage.FileName;
                        string   ext      = image.Substring(image.LastIndexOf("."));
                        string[] goodExts = new string[] { ".jpg", ".jpeg", ".gif", ".png" };

                        if (goodExts.Contains(ext.ToLower()))
                        {
                            image = Guid.NewGuid() + ext;
                            //avatarImage.SaveAs(Server.MapPath("~/Content/Images/Users/" + image));
                            //after resizing the above saving is not needed.

                            //resizing an image file.

                            string savePath       = Server.MapPath("~/Content/Images/Users/");
                            Image  convertedImage = Image.FromStream(avatarImage.InputStream);
                            ImageUtilities.ResizeImage(savePath, image, convertedImage, 500, 100);
                        }
                        else
                        {
                            image = "noImage.jpg";
                        }
                    }
                    newUserDeets.AvatarImage = image;
                    //end image upload

                    FamilySiteEntities db = new FamilySiteEntities();
                    db.UserDetails.Add(newUserDeets);
                    db.SaveChanges();
                    #endregion

                    var 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 this link: <a href=\"" + callbackUrl + "\">link</a>");

                    ViewBag.Link = callbackUrl;


                    return(View("DisplayEmail"));
                }
                AddErrors(result);
            }

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