public async Task<ActionResult> Register(StudentRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Username, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
              
                if (result.Succeeded)
                {
                    var roleresult = UserManager.AddToRole(user.Id, "Student");
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    Student stu = new Student();
                    stu.StudentID = new Guid(user.Id);
                    stu.DateCreated = DateTime.Today;
                    Random rnd = new Random();
                    int filename = rnd.Next(1, 4);
                    stu.ProfileImage = "/Profiles/default/" + filename + ".png";
                    stu.Username = user.UserName;
                    _dbContext.Students.Add(stu);
                    _dbContext.SaveChanges();

                    Notifications notify = new Notifications();
                    notify.ID = Guid.NewGuid();
                    notify.isRead = false;
                    notify.Message = "/Profiles/default/admin.png^Admin^You have successfully created your account. You can click ask question to post your first question.";
                    notify.UserName = stu.Username;
                    notify.postedTime = DateTime.Now;
                    _dbContext.notifications.Add(notify);

                    Notifications notify2 = new Notifications();
                    notify2.ID = Guid.NewGuid();
                    notify2.isRead = false;
                    notify2.Message = "/Profiles/default/admin.png^Admin^We now have Arabic Language support as well.";
                    notify2.UserName = stu.Username;
                    notify2.postedTime = DateTime.Now;
                    _dbContext.notifications.Add(notify2);
                    _dbContext.SaveChanges();

                    // For more information on how to enable account confirmation and password reset please visit http://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", "Students");
                }
                else {

                    var userE = _dbContext.Users.Where(c => c.Email == model.Email).FirstOrDefault();
                    var userU = _dbContext.Users.Where(c => c.UserName == model.Username).FirstOrDefault();
                    if (userE != null)
                        ModelState.AddModelError("", "Email already exists.");
                    if (userU != null)
                        ModelState.AddModelError("", "Username already exists.");
                    ViewBag.error = "registerS";
                    return View("../Home/Index", model);
                }
            }

            // If we got this far, something failed, redisplay form
            // return View(model);
            return RedirectToAction("Index", "Home");
        }
Esempio n. 2
0
 public HomeViewModel()
 {
     Login = new LoginModel();
     RegisterClient = new StudentRegisterModel();
     RegisterExpert = new TutorRegisterModel();
 }