public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser { UserName = model.Username, Email = model.Email, }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (model.SignupAS == "1") { var roleresult = UserManager.AddToRole(user.Id, Status.Student); 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); 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(); } else { var roleresult = UserManager.AddToRole(user.Id, Status.Tutor); Tutor tutor = new Tutor(); tutor.DateOfBirth = DateTime.Today; 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); 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(); } if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser { UserName = model.Username, Email = model.Email, }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (model.SignupAS == "1") { var roleresult = UserManager.AddToRole(user.Id, Status.Student); 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(); } else { var roleresult = UserManager.AddToRole(user.Id, Status.Tutor); Tutor tutor = new Tutor(); tutor.DateOfBirth = DateTime.Today; 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(); } if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }