Exemple #1
0
        public async Task <ActionResult> RegisterStaff(RegisterStaffModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    Staff p = new Staff();
                    p.emailId    = model.Email;
                    p.dob        = model.dob;
                    p.name       = model.name;
                    p.gender     = model.gender;
                    p.contactNo  = model.contactNo;
                    p.type       = model.type;
                    p.dept       = model.dept;;
                    p.password   = model.Password;
                    p.bloodgroup = model.bloodgroup;
                    p.address    = model.address;
                    p.salary     = model.salary;
                    p.appcharge  = model.appcharge;
                    p.desc       = model.desc;
                    hb.Staffs.Add(p);
                    hb.SaveChanges();

                    UserManager.AddToRole(user.Id, p.dept);

                    // For more information on how to enable account confirmation and password reset please visit https://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(RedirectToAction("Index", "Staffs"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <IActionResult> RegisterStaff(RegisterStaffModel model, List <IFormFile> UserImage)
        {
            if (ModelState.IsValid)
            {
                var polly = await userManager.FindByEmailAsync(model.Email);

                if (polly == null)
                {
                    IFormFile f = UserImage.FirstOrDefault();
                    if (f.Length > 0)
                    {
                        if (IsImage(f))
                        {
                            byte[] imageData = null;
                            using (var binary = new MemoryStream())
                            {
                                await f.CopyToAsync(binary);

                                imageData = binary.ToArray();
                            }
                            var user = new CleaningUser
                            {
                                Id          = Guid.NewGuid().ToString(),
                                Fullname    = model.Fullname,
                                Email       = model.Email,
                                PhoneNumber = model.PhoneNumber,
                                UserName    = model.Email,
                                Created     = DateTime.Now
                            };
                            var k = new UserImageRecord
                            {
                                Staff  = user,
                                Upload = imageData
                            };
                            var result = await userManager.CreateAsync(user, model.Password);

                            UserImageImp.AddUserImage(k);

                            if (result.Succeeded)
                            {
                                await userManager.AddToRoleAsync(user, model.UserRoles);

                                HttpContext.Session.SetString("Success", "Registration in Progress,check your email to complete the registration");
                                var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                                string confirmationEmail = Url.Action("ConfirmEmailAddress", "Local",
                                                                      new { token, email = user.Email }, Request.Scheme);
                                string confirm = "Please confirm your account by clicking this link: <a href=\""
                                                 + confirmationEmail + "\">link</a>";

                                EmailService.Send(user.Email, user.Fullname, "Confirmation message", confirm);

                                ModelState.Clear();

                                return(RedirectToAction("RegisterStaff"));
                            }
                        }
                        else
                        {
                            ViewBag.NotRightFormat = "you have not uploaded an image";
                        }
                    }
                    else
                    {
                        ViewBag.noImage = "you have not uploaded an image";
                    }
                }
                else
                {
                    ViewBag.Exist = "Registration in Progress,check your email to complete the registration";
                    string confirm = "you attempted to register an email address that already exist on the system if you would like to login:<a href= \"" + Url.Action("Login", "Account") + "\">click here</a>";
                    EmailService.Send(polly.Email, polly.Fullname, "Account Exist", confirm);
                }
            }
            ViewBag.role = GetUserRole();
            return(View());
        }