Exemple #1
0
        public ActionResult Register()
        {
            var userTypes       = _context.UserTypes.ToList();
            var specializations = _context.Specializations.ToList();
            var groups          = _context.Groups.ToList();
            var semesters       = _context.Semesters.ToList();
            var years           = _context.Years.ToList();

            var viewModel = new NewRegisterUser
            {
                UserTypes       = userTypes,
                Specializations = specializations,
                Groups          = groups,
                Semesters       = semesters,
                Years           = years
            };

            return(View(viewModel));
        }
Exemple #2
0
        public async Task <ActionResult> Register(NewRegisterUser newUser)
        {
            if (ModelState.IsValid)
            {
                var model = newUser.RegisterViewModel;

                if (model.UserTypeId == 1)
                {
                    var user = new ApplicationUser
                    {
                        UserName   = model.Email,
                        Email      = model.Email,
                        FirstName  = model.FirstName,
                        LastName   = model.LastName,
                        UserTypeId = model.UserTypeId
                    };


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

                    if (result.Succeeded)
                    {
                        await UserManager.AddToRoleAsync(user.Id, "StudentRole");


                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        var student = new Student
                        {
                            SpecializationId  = model.SpecializationId,
                            GroupId           = model.GroupId,
                            SemesterId        = model.SemesterId,
                            YearId            = model.YearId,
                            ApplicationUserId = user.Id
                        };
                        _context.Students.Add(student);
                        _context.SaveChanges();

                        // 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/" + user.Id, "Students"));
                    }
                    AddErrors(result);
                }
                if (model.UserTypeId == 2)
                {
                    var teacherCode = _context.Codes.SingleOrDefault(c => c.TeacherEmail == model.Email);

                    if (model.TeacherCode == teacherCode.CodeValue)
                    {
                        var user = new ApplicationUser
                        {
                            UserName   = model.Email,
                            Email      = model.Email,
                            FirstName  = model.FirstName,
                            LastName   = model.LastName,
                            UserTypeId = model.UserTypeId
                        };

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

                        if (result.Succeeded)
                        {
                            await UserManager.AddToRoleAsync(user.Id, "TeacherRole");


                            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                            var teacher = new Teacher
                            {
                                ApplicationUserId = user.Id,
                                RegisterCode      = model.TeacherCode
                            };
                            _context.Teachers.Add(teacher);
                            _context.SaveChanges();

                            // 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/" + user.Id, "Teachers"));
                        }
                        AddErrors(result);
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid teacher Code.");
                        return(View(newUser));
                    }
                }


                //var user = new ApplicationUser
                //{
                //    UserName = model.Email,
                //    Email = model.Email,
                //    FirstName = model.FirstName,
                //    LastName = model.LastName,

                //};

                //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(newUser);
            return(RedirectToAction("Register", "Account"));
        }