Ejemplo n.º 1
0
        public async Task <IActionResult> CreateStudentAccount(CreateStudentAccountViewModel model)
        {
            if (context.Students.Any(x => x.NumberOfStudentBook == model.NumberOfStudentBook))
            {
                ModelState.AddModelError("", $"Student with number {model.NumberOfStudentBook} is alredy exist.");
            }

            if (ModelState.IsValid)
            {
                StudentUser student = new StudentUser()
                {
                    UserName            = model.FirstName + "_" + model.SecondName + "_" + model.NumberOfStudentBook,
                    Email               = model.Email,
                    GroupID             = model.GroupID,
                    FirstName           = model.FirstName,
                    LastName            = model.SecondName,
                    NumberOfStudentBook = model.NumberOfStudentBook
                };


                var result = await Manager.CreateAsync(student);

                if (result.Succeeded)
                {
                    var user = await Manager.FindByEmailAsync(student.Email) as StudentUser;

                    try
                    {
                        var token = await Manager.GenerateEmailConfirmationTokenAsync(user);

                        string link      = $@"{(HttpContext.Request.IsHttps?"https":"http")}://{HttpContext.Request.Host}{Url.Action("Registration", "Account", new { area = "Student", Token = token, Magic = student.Id })}";
                        var    mailmodel = new StRgsMailModel {
                            user = user, link = link
                        };
                        var msg = MailSender.HtmlEmailMesage("EmailRegistration", mailmodel);

                        await MailSender.SendEmailAsync(student.Email, "Registration", msg);


                        await Manager.AddToRoleAsync(student, "Student");

                        return(Redirect(model.returnUrl));
                    }
                    catch (Exception e)
                    {
                        await Manager.DeleteAsync(user);

                        ModelState.AddModelError("", e.Message);
                    }
                }
                else
                {
                    foreach (var item in result.Errors)
                    {
                        ModelState.AddModelError("", item.Description);
                    }
                }
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        public IActionResult CreateStudentAccount(int GroupID, string returnUrl)
        {
            var model = new CreateStudentAccountViewModel {
                GroupID = GroupID, returnUrl = returnUrl
            };

            return(View(model));
        }