コード例 #1
0
        public async Task<ActionResult> RegisterTutor(TutorRegisterModel 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, "Tutor");
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    Tutor tutor = Mapper.Map<TutorRegisterModel, Tutor>(model);
                    tutor.TutorID = new Guid(user.Id);
                    tutor.DateCreated = DateTime.Today;
                    tutor.IsCompletedProfile = false;
                    tutor.Username = user.UserName;
                    Random rnd = new Random();
                    int filename = rnd.Next(1, 4);
                    tutor.ProfileImage = "/Profiles/default/"+filename+".png";
                    _dbContext.Tutors.Add(tutor);
                    _dbContext.SaveChanges();

                    Notifications notify = new Notifications();
                    notify.ID = Guid.NewGuid();
                    notify.isRead = false;
                    notify.Message = "/Profiles/default/admin.png^Admin^Please complete your profile so that you have full access.";
                    notify.UserName = tutor.Username;
                    notify.postedTime = DateTime.Now;
                    _dbContext.notifications.Add(notify);
                    _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", "Tutors");
                }
                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 = "registerT";
                    return View("../Home/Index", model);
                }
               
            }

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