public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new IdentityUser { UserName = model.UserName, UserType = (int)UserTypes.ProspectiveStudent, IsApproved = true, IsLockedOut = false, IsFirstTimeUser = false, Email = model.UserName }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //from here we create prospective student who shall be attached to this user... User coreUser = _accountRepo.GetUserByName(model.UserName); var prospectiveStudent = new ProspectiveStudent { User = coreUser, Person = new Person { Surname = model.Surname, GivenName = model.FirstName, EmailAddress = model.UserName }, CreatedOn = DateTime.Now }; _coreRepo.SaveOrUpdate(prospectiveStudent); await SignInAsync(user, false); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
//==================================================================================// // Store Prospect Student information // // // // Register the prospective student information if it is // // empty. Otherwise edit it. // // // // Note: If the edit is true then the account is being edited. // // Otherwise it is being registered. // //==================================================================================// public static bool StoreProspectData(ProspectModel prospect, ref bool edit) { int UserId = WebSecurity.GetUserId(prospect.EmailAddress); ProspectiveStudent CurrentStudent; edit = false; try { using (ITintheDTestTableEntities context = new ITintheDTestTableEntities()) { // Put everything we find in the database in the var variable. All the // information will be gotten using the User ID. var ProspectData = from r in context.ProspectiveStudent where r.UserId == UserId select r; // If the user has some information then edit it. // Otherwise register the account. if (ProspectData.Count() > 0 && UserId > 0) { edit = true; CurrentStudent = ProspectData.FirstOrDefault(); CurrentStudent.Status = prospect.AccountStatus; CurrentStudent.UserId = UserId; CurrentStudent.Name = prospect.Name; CurrentStudent.Telephone = prospect.Telephone; CurrentStudent.EmailAddress = prospect.EmailAddress; CurrentStudent.DesiredCareerPath = prospect.DesiredCareerPath; CurrentStudent.Gender = prospect.Gender; CurrentStudent.ProspectiveStudentTextField = prospect.ProspectiveStudentTextField; // Store the avatar if it is supplied. if (prospect.ImageFile != null) { UserImage image = new UserImage(); using (MemoryStream ms = new MemoryStream()) { prospect.ImageFile.InputStream.CopyTo(ms); image.FileContent = ms.ToArray(); image.FileName = Path.GetFileName(prospect.ImageFile.FileName); image.ContentType = prospect.ImageFile.ContentType; image.ContentLength = prospect.ImageFile.ContentLength; DatabaseHelper.UploadImage(image, CurrentStudent.UserId); CurrentStudent.ImageUploaded = "Yes"; prospect.ImageUploaded = "Yes"; } } // Store the new resume if it is supplied and not empty. if (prospect.ResumeFile != null && prospect.ResumeFile.ContentLength > 0) { ProspectiveStudentResume Resume = new ProspectiveStudentResume(); using (MemoryStream ms = new MemoryStream()) { prospect.ResumeFile.InputStream.CopyTo(ms); Resume.FileContent = ms.ToArray(); Resume.FileName = Path.GetFileName(prospect.ResumeFile.FileName); Resume.ContentType = prospect.ResumeFile.ContentType; Resume.ContentLength = prospect.ResumeFile.ContentLength; DatabaseHelper.UploadResume(Resume, prospect); CurrentStudent.ResumeUploaded = "Yes"; prospect.ResumeUploaded = "Yes"; } } // Store the new transcript if it is supplied and not empty. if (prospect.TranscriptFile != null && prospect.TranscriptFile.ContentLength > 0) { ProspectiveStudentTranscript Transcript = new ProspectiveStudentTranscript(); using (MemoryStream ts = new MemoryStream()) { prospect.TranscriptFile.InputStream.CopyTo(ts); Transcript.FileContent = ts.ToArray(); Transcript.FileName = Path.GetFileName(prospect.TranscriptFile.FileName); Transcript.ContentType = prospect.TranscriptFile.ContentType; Transcript.ContentLength = prospect.TranscriptFile.ContentLength; DatabaseHelper.UploadTranscript(Transcript, prospect); CurrentStudent.TranscriptUploaded = "Yes"; prospect.TranscriptUploaded = "Yes"; } } } else { CurrentStudent = new ProspectiveStudent(); CurrentStudent.Status = prospect.AccountStatus; CurrentStudent.Name = prospect.Name; CurrentStudent.Telephone = prospect.Telephone; CurrentStudent.EmailAddress = prospect.EmailAddress; CurrentStudent.DesiredCareerPath = prospect.DesiredCareerPath; CurrentStudent.Gender = prospect.Gender; CurrentStudent.ProspectiveStudentTextField = prospect.ProspectiveStudentTextField; context.AddToProspectiveStudent(CurrentStudent); } try { // If the account is edited then save changes. Otherwise register the account. if (edit == false) { WebSecurity.CreateUserAndAccount(prospect.EmailAddress, prospect.Password); // User is automatically logged in here. WebSecurity.Login(prospect.EmailAddress, prospect.Password); DatabaseHelper.AddUserToRole(prospect.EmailAddress, "Student"); CurrentStudent.UserId = WebSecurity.GetUserId(prospect.EmailAddress); // Store the avatar if it is supplied. if (prospect.ImageFile != null) { UserImage image = new UserImage(); using (MemoryStream ms = new MemoryStream()) { prospect.ImageFile.InputStream.CopyTo(ms); image.FileContent = ms.ToArray(); image.FileName = Path.GetFileName(prospect.ImageFile.FileName); image.ContentType = prospect.ImageFile.ContentType; image.ContentLength = prospect.ImageFile.ContentLength; DatabaseHelper.UploadImage(image, CurrentStudent.UserId); CurrentStudent.ImageUploaded = "Yes"; prospect.ImageUploaded = "Yes"; } } else { CurrentStudent.ImageUploaded = "No"; prospect.ImageUploaded = "No"; } // Store the resume if it is supplied and not empty. if (prospect.ResumeFile != null && prospect.ResumeFile.ContentLength > 0) { ProspectiveStudentResume Resume = new ProspectiveStudentResume(); using (MemoryStream ms = new MemoryStream()) { prospect.ResumeFile.InputStream.CopyTo(ms); Resume.FileContent = ms.ToArray(); Resume.FileName = Path.GetFileName(prospect.ResumeFile.FileName); Resume.ContentType = prospect.ResumeFile.ContentType; Resume.ContentLength = prospect.ResumeFile.ContentLength; DatabaseHelper.UploadResume(Resume, prospect); CurrentStudent.ResumeUploaded = "Yes"; prospect.ResumeUploaded = "Yes"; } } else { CurrentStudent.ResumeUploaded = "No"; prospect.ResumeUploaded = "No"; } // Store the resume if it is supplied and not empty. if (prospect.TranscriptFile != null && prospect.TranscriptFile.ContentLength > 0) { ProspectiveStudentTranscript Transcript = new ProspectiveStudentTranscript(); using (MemoryStream ts = new MemoryStream()) { prospect.TranscriptFile.InputStream.CopyTo(ts); Transcript.FileContent = ts.ToArray(); Transcript.FileName = Path.GetFileName(prospect.TranscriptFile.FileName); Transcript.ContentType = prospect.TranscriptFile.ContentType; Transcript.ContentLength = prospect.TranscriptFile.ContentLength; DatabaseHelper.UploadTranscript(Transcript, prospect); CurrentStudent.TranscriptUploaded = "Yes"; prospect.TranscriptUploaded = "Yes"; } } else { CurrentStudent.TranscriptUploaded = "No"; prospect.TranscriptUploaded = "No"; } } context.SaveChanges(); return true; } catch { return false; } } } catch { return false; } }