예제 #1
0
        public async Task <ActionResult> Register(RegisterViewModel 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);

                    using (Entities4 db = new Entities4())
                    {
                        if (model.Role == "Student")
                        {
                            var     userid        = db.AspNetUsers.Where(x => x.Email == model.Email).FirstOrDefault();
                            string  userid1       = userid.Id;
                            Student studentDetail = new Student();
                            studentDetail.StudentName     = model.Name;
                            studentDetail.StudentEmail    = model.Email;
                            studentDetail.StudentPhoneNo  = model.PhoneNo;
                            studentDetail.StudentLocation = model.Location;
                            studentDetail.StudentDob      = model.DOB;
                            studentDetail.StudentInterest = model.Interest;
                            studentDetail.AdminId         = 1;
                            studentDetail.user_id         = userid1;
                            db.Students.Add(studentDetail);
                            db.SaveChanges();
                            using (Entities2 db1 = new Entities2())
                            {
                                AspNetUserRole role = new AspNetUserRole();
                                role.UserId = userid1;
                                role.RoleId = "2";
                                db1.AspNetUserRoles.Add(role);
                                db1.SaveChanges();
                            }
                        }
                        if (model.Role == "Instructor")
                        {
                            var        userid           = db.AspNetUsers.Where(x => x.Email == model.Email).FirstOrDefault();
                            string     userid1          = userid.Id;
                            Instructor instructorDetail = new Instructor();
                            instructorDetail.InstructorName          = model.Name;
                            instructorDetail.InstructorDob           = model.DOB;
                            instructorDetail.InstructorEmailId       = model.Email;
                            instructorDetail.InstructorLocation      = model.Location;
                            instructorDetail.InstructorQualification = model.Qualification;
                            instructorDetail.AdminId = 1;
                            instructorDetail.user_id = userid1;
                            db.Instructors.Add(instructorDetail);
                            db.SaveChanges();
                            using (Entities2 db1 = new Entities2())
                            {
                                AspNetUserRole role = new AspNetUserRole();
                                role.UserId = userid1;
                                role.RoleId = "1";
                                db1.AspNetUserRoles.Add(role);
                                db1.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", "Home"));
                }
                AddErrors(result);
            }

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