Esempio n. 1
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName
                };

                var result = await UserManager.CreateAsync(user, model.Password);

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

                    // 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("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (FileHelper.IsWebFriendlyImage(model.Avatar))
                {
                    var user = new ApplicationUser
                    {
                        FirstName  = model.FirstName,
                        LastName   = model.LastName,
                        UserName   = model.Email,
                        Email      = model.Email,
                        AvatarPath = WebConfigurationManager.AppSettings["DefaultAvatarPath"]
                    };

                    if (model.Avatar != null)
                    {
                        var fileName = FileHelper.MakeUnique(model.Avatar);

                        model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                        user.AvatarPath = "/Avatars/" + fileName;
                    }
                    var result = await UserManager.CreateAsync(user, model.Password);

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

                        // 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 }, protocol: Request.Url.Scheme);
                        try
                        {
                            var from  = "BugTracker<*****@*****.**>";
                            var email = new MailMessage(from, model.Email)
                            {
                                Subject    = "Confirm your Account",
                                Body       = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>",
                                IsBodyHtml = true
                            };
                            var svc = new EmailService();
                            await svc.SendAsync(email);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            await Task.FromResult(0);
                        }


                        return(RedirectToAction("ConfirmationSent", "Account"));
                    }
                    AddErrors(result);
                }
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 3
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, AvatarPath = "/Avatars/purpleuser.png"
                };

                if (model.Avatar != null)
                {
                    if (ImageUploadValidator.IsWebFriendlyImage(model.Avatar))
                    {
                        var fileName     = FileStamp.MakeUnique(model.Avatar.FileName);
                        var serverFolder = WebConfigurationManager.AppSettings["DefaultServerFolder"];
                        model.Avatar.SaveAs(Path.Combine(Server.MapPath(serverFolder), fileName));
                        user.AvatarPath = $"{serverFolder}{fileName}";
                    }
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (user.Roles.FirstOrDefault() == null)
                {
                    roleHelper.AddUserToRole(user.Id, "Unassigned");
                }
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, protocol: Request.Url.Scheme);
                    try
                    {
                        var from  = "Bug Tracker<*****@*****.**>";
                        var email = new MailMessage(from, model.Email)
                        {
                            Subject    = "Confirm Your Acount",
                            Body       = "Please confirm your email by clicking <a href=\"" + callbackUrl + "\">here</a> ",
                            IsBodyHtml = true
                        };
                        var svc = new EmailService();
                        await svc.SendAsync(email);

                        //return View("EmailConfirmed");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }
                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 4
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    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);

                    // 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
                    try
                    {
                        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>");

                        var emailAddress = WebConfigurationManager.AppSettings["emailFrom"];
                        var emailFrom    = $"bug Tracker <{emailAddress}>";

                        var email = new MailMessage(emailFrom, model.Email)
                        {
                            Subject    = "Confirm Your Account",
                            Body       = "Please confirm your account by clicking < a href =\"" + callbackUrl + "\">here</a>",
                            IsBodyHtml = true
                        };

                        var emailSvr = new EmailService();
                        await emailSvr.SendAsync(email);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }

                    TempData["UnconfirmedEmailError"] = "You should have recieved an email asking you to confirm your new account setup. Please find the email and confirm before trying to login. ";
                    return(RedirectToAction("Login", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 5
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    UserName    = model.Email,
                    Email       = model.Email,
                    AvatarPath  = "/Images/avatar-placeholder.png"
                };


                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    try
                    {
                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                        var emailAddress = WebConfigurationManager.AppSettings["EmailFrom"];

                        var emailFrom = $"BugTracker <{emailAddress}>";

                        var email = new MailMessage(emailFrom, model.Email)
                        {
                            Subject    = "Confirm Your Account",
                            Body       = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>",
                            IsBodyHtml = true
                        };

                        var emailSvc = new EmailService();
                        await emailSvc.SendAsync(email);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }

                    TempData["UnconfirmedEmailError"] = "Please check your email to verify your account before logging in.";
                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }
            return(View(model));
        }
Esempio n. 6
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.DisplayName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

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

                    // 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 }, protocol: Request.Url.Scheme);
                    try
                    {
                        var from  = "Richard's ASP.Net MVC Blog Admin<*****@*****.**>";
                        var email = new MailMessage(from, model.Email)
                        {
                            Subject    = "Confirm your email.",
                            Body       = $"Hello {model.FirstName}, please click <a href =\"" + callbackUrl + "\">here</a> to confirm your email address.",
                            IsBodyHtml = true
                        };
                        var svc = new EmailService();
                        await svc.SendAsync(email);

                        ModelState.Clear();
                        ViewBag.Success = "true";
                        ViewBag.Message = "Successfully submitted.  Please check your email to complete your registration.";
                        return(View());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }


                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Login", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    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);

                    // 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>");
                    var from = $"Bug-A-Boo<{ConfigurationManager.AppSettings["emailfrom"]}>";
                    try
                    {
                        var email = new MailMessage(from, model.Email)
                        {
                            Subject    = "Confirmation",
                            Body       = "Confirm your account<a class='btn btn-primary' href=\"" + callbackUrl + "\"> Confirm </a>",
                            IsBodyHtml = true
                        };
                        var svc = new PersonalEmailService();
                        await svc.SendAsync(email);

                        ViewBag.Message = "You Must Confirm Your Email To Login";
                        //return RedirectToAction("StartLogin", "Account");
                        return(View("Info"));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(RedirectToAction("StartLogin", "Home"));;
        }
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            // Register everyone with a efault Avatar in case thy choose not to use one.
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                    DisplayName    = model.DisplayName,
                    UserName       = model.Email,
                    Email          = model.Email,
                    EmailConfirmed = true,
                    AvatarPath     = "/Avatars/default.png"
                };

                //Check to see if the registered user chose a custom Avatar
                if (model.Avatar != null)
                {
                    //Check to make sure chose file is an image and can be used.

                    var justFileName = Path.GetFileNameWithoutExtension(model.Avatar.FileName);
                    justFileName = StringUtilities.URLFriendly(justFileName);
                    justFileName = $"{justFileName}-{DateTime.Now.Ticks}";
                    justFileName = $"{justFileName}{Path.GetExtension(model.Avatar.FileName)}";

                    user.AvatarPath = $"/Avatars/{justFileName}";
                    model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), justFileName));
                }

                var result = await UserManager.CreateAsync(user, model.Password);

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

                    // 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("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 9
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    AvatarUrl = "/Avatars/default-user-icon-8.jpg"
                };

                // Check to see if the incoming Avatar property is null
                //if (ImageHelper.IsValidAvatar(model.Avatar))
                //{
                //    var fileName = Path.GetFileNameWithoutExtension(model.Avatar.FileName);
                //    var fileExtension = Path.GetExtension(model.Avatar.FileName);
                // 1. Add DateTime stamp to end of filename
                // 2. Run that through slugmaker
                // 3. Map path
                // 4. Save complete file
                //    var fileWithDate = $"{fileName}-{DateTime.Now.Ticks}";
                //    var slugFileName = Utilities.CreateSlug(fileWithDate);
                //    var formattedMedia = $"{slugFileName}{fileExtension}";
                //    model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), formattedMedia));
                //    user.AvatarUrl = "/Avatars/" + formattedMedia;
                //}
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var userId = UserManager.FindByEmail(model.Email).Id;
                    UserManager.AddToRole(userId, "Lobbyist");
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // 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("Lobby", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 10
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    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);

                    // 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>");


                    var from  = $"Ashton's Blog<{WebConfigurationManager.AppSettings["emailfrom"]}>";
                    var email = new MailMessage(from, model.Email)
                    {
                        Subject    = "Confirm Your Account",
                        Body       = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>",
                        IsBodyHtml = true
                    };

                    var svc = new EmailService();
                    await svc.SendAsync(email);


                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 11
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };

                if (model.Avatar != null)
                {
                    if (FileUploadValidator.IsWebFriendlyImage(model.Avatar))
                    {
                        var fileName = FileStamp.MakeUnique(model.Avatar.FileName);

                        var serverFolder = WebConfigurationManager.AppSettings["DefaultServerFolder"];
                        model.Avatar.SaveAs(Path.Combine(Server.MapPath("~" + serverFolder), fileName));
                        user.AvatarPath = $"{serverFolder}/{fileName}";
                    }
                }

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    rolesHelper.AddUserToRole(user.Id, "New User");
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // 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("BuildHouse", "Households"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 12
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName   = model.Email,
                    Email      = model.Email,
                    FirstName  = model.FirstName,
                    LastName   = model.LastName,
                    AvatarPath = "/Avatars/blank-user.jpg"
                };

                if (image != null && ImageUploadValidator.IsWebFriendlyImage(image))
                {
                    var justFileName = System.IO.Path.GetFileNameWithoutExtension(image.FileName);
                    justFileName = StringUtilities.URLFriendly(justFileName);
                    justFileName = $"{justFileName}--{DateTime.Now.Ticks}";
                    justFileName = $"{justFileName}{System.IO.Path.GetExtension(image.FileName)}";

                    user.AvatarPath = "/Avatars/" + justFileName;
                    image.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Avatars/"), justFileName));
                }

                var result = await UserManager.CreateAsync(user, model.Password);

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

                    // 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", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 13
0
        public ActionResult AcceptInvite(string email, string code)
        {
            // Use this incoming info to grab the appropriate invitation from the invite table
            var guid   = Guid.Parse(code);
            var invite = db.Invitations.FirstOrDefault(i => i.RecipientEmail == email && i.Code == guid);

            if (invite != null && invite.Valid)
            {
                var extendedRegVM = new ExtendedRegisterViewModel {
                    Email = email
                };
                ViewBag.HouseholdId = invite.HouseholdId;
                return(View("Register", extendedRegVM));
            }
            else
            {
                // Customize Error View
                return(View("Error"));
            }
        }
        public ActionResult AcceptInvite(string email, string code)
        {
            //grabs the appropriate invite from the invite table
            var guid   = Guid.Parse(code);
            var invite = db.Invites.FirstOrDefault(i => i.RecipientEmail == email && i.Code == guid);

            if (invite != null && invite.Valid)
            {
                var inviteRegVm = new ExtendedRegisterViewModel {
                    Email = email, Code = code
                };
                ViewBag.HouseholdId = invite.HouseholdId;
                return(View("Register", inviteRegVm));
            }
            else
            {
                //tells user an error has occured
                return(View("InviteError"));
            }
        }
Esempio n. 15
0
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName  = model.Email,
                    Email     = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    AvatarUrl = "Avatars/default_avatar2.png"
                };

                //Check to see if the incoming Avatar property is null
                if (ImageHelper.IsValidAvatar(model.Avatar))
                {
                    //This is the actual filename after being run through the slugmaker
                    var fileName = ImageHelper.CreateSlug(Path.GetFileName(model.Avatar.FileName));
                    fileName = $"{fileName}_{DateTime.Now.Ticks}";
                    model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                    user.AvatarUrl = "/Avatars/" + fileName;
                }

                var result = await UserManager.CreateAsync(user, model.Password);

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


                    return(RedirectToAction("Lobby", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 16
0
        public ActionResult Register()
        {
            var placeHolder = new ExtendedRegisterViewModel();

            return(View(placeHolder));
        }
Esempio n. 17
0
        public async Task <IActionResult> Register(string PrimaEmail, ExtendedRegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                Korisnik korisnik = new Korisnik
                {
                    Ime     = model.Ime,
                    Prezime = model.Prezime,
                    Email   = model.Email,
                    Lozinka = model.Password,
                    TelBroj = model.TelBroj,
                    Oib     = model.Oib
                };

                if (PrimaEmail == "on")
                {
                    korisnik.PrimaEmail = true;
                }
                else
                {
                    korisnik.PrimaEmail = false;
                }
                _context.Korisnik.Add(korisnik);
                _context.SaveChanges();

                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, IdKorisnik = korisnik.Id
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (model.Uloga == "N")
                {
                    Narucitelj narucitelj = new Narucitelj
                    {
                        IdKorisnik = korisnik.Id
                    };
                    _context.Narucitelj.Add(narucitelj);

                    await _userManager.AddToRoleAsync(user, "narucitelj");
                }
                else if (model.Uloga == "P")
                {
                    Prijevoznik prijevoznik = new Prijevoznik
                    {
                        IdKorisnik = korisnik.Id
                    };
                    _context.Prijevoznik.Add(prijevoznik);

                    await _userManager.AddToRoleAsync(user, "prijevoznik");
                }
                else if (model.Uloga == "A")
                {
                    await _userManager.AddToRoleAsync(user, "administrator");
                }

                _context.SaveChanges();


                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");

                    if (PrimaEmail == "on")
                    {
                        var smtpClient = new SmtpClient
                        {
                            Host        = "smtp.gmail.com",
                            Port        = 587,
                            EnableSsl   = true,
                            Credentials = new NetworkCredential("*****@*****.**", "carryon1")
                        };

                        using (var message = new MailMessage("*****@*****.**", user.Email)
                        {
                            Subject = "Uspješna registracija",
                            Body = "Uspješno ste se registrirali."
                        })
                        {
                            await smtpClient.SendMailAsync(message);
                        }
                    }
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 18
0
        public ActionResult Register()
        {
            var userModel = new ExtendedRegisterViewModel();

            return(View(userModel));
        }
Esempio n. 19
0
        public async Task <IActionResult> ExtendedRegistration([FromBody] ExtendedRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                await _unitOfWork.BeginTransactionAsync();

                try
                {
                    var request = (await _unitOfWork.RegistrationRequest.Find(r => r.Code.Equals(model.Ref)))?.FirstOrDefault();

                    if (request is null)
                    {
                        throw new Exception("Неверный код");
                    }

                    var department = (await _unitOfWork.Department.Find(d => d.Name.ToLower().Equals(request.Department.ToLower())))?.FirstOrDefault();

                    if (department is null)
                    {
                        var newDepartment = new Department
                        {
                            Name = request.Department,
                        };

                        await _unitOfWork.Department.Insert(newDepartment);

                        await _unitOfWork.SaveChangesAsync();

                        department = newDepartment;
                    }

                    var position = (await _unitOfWork.Position.Find(p =>
                                                                    p.Name.ToLower().Equals(request.Position.ToLower())))?.FirstOrDefault();

                    if (position is null)
                    {
                        var newPosition = new Position
                        {
                            Name = request.Position
                        };

                        await _unitOfWork.Position.Insert(newPosition);

                        await _unitOfWork.SaveChangesAsync();

                        position = newPosition;
                    }


                    var newEmployee = new ApplicationUser
                    {
                        UserName   = request.Email,
                        Email      = request.Email,
                        FirstName  = request.FirstName,
                        LastName   = request.LastName,
                        Patronymic = request.Patronymic,
                        Department = department,
                        Position   = position
                    };

                    newEmployee.CompanyEmployees = new List <CompanyEmployee>
                    {
                        new CompanyEmployee
                        {
                            CompanyId = request.CompanyId,
                            User      = newEmployee
                        }
                    };

                    // Создаем нового пользователя
                    var result = await _userManager.CreateAsync(newEmployee, model.Password);

                    if (result.Succeeded)
                    {
                        await _unitOfWork.RegistrationRequest.Delete(request.RegistrationRequestId);

                        await _unitOfWork.SaveChangesAsync();

                        // Создаем уведомление о присоединении нового сотрудника к компании
                        CompanyNotification newCompanyNotification = new CompanyNotification
                        {
                            Author      = newEmployee,
                            CompanyId   = request.CompanyId,
                            NewEmployee = newEmployee,
                            Type        = CompanyNotificationType.EmployeeJoin,
                            DateTime    = DateTime.Now
                        };

                        await _unitOfWork.CompanyNotification.Insert(newCompanyNotification);

                        _unitOfWork.Commit();
                        return(Ok());
                    }
                    else
                    {
                        _unitOfWork.Rollback();
                        return(BadRequest(result.Errors));
                    }
                }
                catch (Exception ex)
                {
                    _unitOfWork.Rollback();
                    return(BadRequest(ex.Message));
                }
            }

            string messages = string.Join("; ", ModelState.Values
                                          .SelectMany(x => x.Errors)
                                          .Select(x => x.ErrorMessage));

            return(BadRequest(messages));
        }
        public async Task <ActionResult> Register(ExtendedRegisterViewModel model, int?houseHoldId)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName       = model.Email,
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                    Email          = model.Email,
                    EmailConfirmed = true,
                    AvatarPath     = "/Avatars/download.jpg",
                    HouseholdId    = houseHoldId,
                };

                if (model.Avatar != null)
                {
                    var justFileName = Path.GetFileNameWithoutExtension(model.Avatar.FileName);
                    justFileName = StringUtilities.URLFriendly(justFileName);
                    justFileName = $"{justFileName}-{DateTime.Now.Ticks}";
                    justFileName = $"{justFileName}{Path.GetExtension(model.Avatar.FileName)}";

                    var fileName = Path.GetFileName(model.Avatar.FileName);
                    model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), justFileName));
                    user.AvatarPath = $"/Avatars/{justFileName}";
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    if (houseHoldId != null && !string.IsNullOrEmpty(model.Code))
                    {
                        rolehelper.AddUserToRole(user.Id, "Member");
                    }
                    try
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        // 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>");
                        //var from = $"Complication Station<{WebConfigurationManager.AppSettings["emailfrom"]}>";
                        //var emailAddress = WebConfigurationManager.AppSettings["EmailFrom"];
                        //var emailFrom = $"Falco Issue Tracker <{emailAddress}>";

                        //var email = new MailMessage(emailFrom, model.Email)
                        //{
                        //    Subject = "Confirm Email",
                        //    Body = $"Please confirm your account by clicking <a href=\"{callbackUrl}\">here</a>",
                        //    IsBodyHtml = true
                        //};
                        var EmailSvc = new EmailService();
                        //await EmailSvc.SendAsync(email);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        await Task.FromResult(0);
                    }
                    //TempData["UnconfirmedEmail"] = "Please check your email and verify your account.";
                    if (string.IsNullOrEmpty(model.Code))
                    {
                        return(RedirectToAction("Dashboard", "Households"));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }