public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _membersService.AddMember( new ShoppingCart.Application.ViewModels.MemberViewModel() { Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName }); await _userManager.AddToRoleAsync(user, "User"); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); await _userManager.AddToRoleAsync(user, "Teacher"); if (result.Succeeded) { _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); Tuple <string, string> keys = CryptographicHelper.GenerateAsymmetricKeys(); _membersService.AddMember(new SecuringApplicationsAssignment.Application.ViewModels.MemberViewModel { Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, //Generating Keys for teacher PrivateKey = keys.Item2, PublicKey = keys.Item1 } ); var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = userId, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); // If account confirmation is required, we need to show the link if we don't have a real email sender if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email })); } await _signInManager.SignInAsync(user, isPersistent : false, info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } ProviderDisplayName = info.ProviderDisplayName; ReturnUrl = returnUrl; return(Page()); }
public IActionResult AddStudent(AddStudentModel model) { if (!ModelState.IsValid) { TempData["warning"] = "Invalid"; return(View()); } if (_membersService.GetMember(model.MemberModel.Email) != null) { TempData["warning"] = "Student already exists"; return(RedirectToAction("Index", "Assignments")); } string randomPassword = GenerateRandomPassword(); var user = new ApplicationUser { UserName = model.MemberModel.Email, Email = model.MemberModel.Email }; var result = _userManager.CreateAsync(user, randomPassword); if (result.Result.Succeeded) { _userManager.AddToRoleAsync(user, "Student"); EmailModel em = new EmailModel(); em.Email = "*****@*****.**"; em.To = model.MemberModel.Email; model.EmailModel = em; using (MailMessage mm = new MailMessage(model.EmailModel.Email, model.EmailModel.To)) { mm.Subject = "Login Credentials"; mm.Body = "You have been assigned an account. Username is: " + em.To + " and your password is : " + randomPassword; mm.IsBodyHtml = false; using (SmtpClient smtp = new SmtpClient()) { smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(model.EmailModel.Email, "74bf*XBG^0ga"); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mm); ViewBag.Message = "Email sent"; } } model.MemberModel.TeacherEmail = User.Identity.Name; Tuple <string, string> keys = CryptographicHelper.GenerateAsymmetricKeys(); model.MemberModel.PublicKey = keys.Item1; model.MemberModel.PrivateKey = keys.Item2; _membersService.AddMember(model.MemberModel); } return(RedirectToAction("Index", "Assignments")); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName }; Input.Password = RandomString(); var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "student"); AsymmetricKeys encKeys = new AsymmetricKeys(); encKeys = GenerateAsymmetricKeys(); MemberViewModel member = new MemberViewModel { Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, LecturerEmail = User.Identity.Name, PublicKey = encKeys.PublicKey, PrivateKey = encKeys.PrivateKey }; _membersService.AddMember(member); SendEmail(Input.Email, Input.Password); /*_logger.LogInformation("User created a new account with password."); * * var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); * code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); * var callbackUrl = Url.Page( * "/Account/ConfirmEmail", * pageHandler: null, * values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, * protocol: Request.Scheme); * * await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", * $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); * * if (_userManager.Options.SignIn.RequireConfirmedAccount) * { * return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }); * } * else * { * await _signInManager.SignInAsync(user, isPersistent: false); * return LocalRedirect(returnUrl); * }*/ } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, Address = Input.Address }; string myval = Guid.NewGuid().ToString("d").Substring(1, 4).ToLower(); string myval2 = Guid.NewGuid().ToString("d").Substring(1, 4).ToUpper() + "@"; string resultRandom = myval + myval2; var result = await _userManager.CreateAsync(user, resultRandom); if (result.Succeeded) { AsymmetricKeys encKeys = new AsymmetricKeys(); encKeys = Encryption.GenerateAsymmetricKeys(); MemberViewModel memberViewModel = new MemberViewModel(); memberViewModel.FirstName = Input.FirstName; memberViewModel.LastName = Input.LastName; memberViewModel.Email = Input.Email; memberViewModel.teacherEmail = User.Identity.Name; memberViewModel.publicKey = encKeys.PublicKey; memberViewModel.privateKey = encKeys.PrivateKey; _membersservice.AddMember(memberViewModel); await _userManager.AddToRoleAsync(user, "student"); Input.Password = resultRandom; sendEmail(Input.Email, resultRandom); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } // foreach (var error in result.Errors) // { // ModelState.AddModelError(string.Empty, error.Description); // } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName /*Address= Input.Address*/ }; string validChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-"; Random random = new Random(); char[] chars = new char[15]; for (int i = 0; i < 15; i++) { chars[i] = validChars[random.Next(0, validChars.Length)]; } string pass = new string(chars); Input.Password = pass; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "STUDENT"); AsymmetricKeys keys = new AsymmetricKeys(); keys = Encryption.GenerateAsymmetricKeys(); string teacherEmail = User.Identity.Name; _membersService.AddMember( new ShoppingCart.Application.ViewModels.MemberViewModel() { Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, PublicKey = keys.PublicKey, PrivateKey = keys.PrivateKey, TeacherEmail = teacherEmail } ); _logger.LogInformation("User created a new account with password."); try{ MailMessage message = new MailMessage("*****@*****.**", Input.Email); message.Subject = "User Registered"; message.Body = "--- Welcome User ----\n " + "Please find your account details below \n" + "Username: "******"\n" + "Password : "******"smtp-mail.outlook.com", 587); System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential("*****@*****.**", "mcast262199"); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = basicCredential1; client.Send(message); }catch (Exception ex) { throw ex; } return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public Member AddMember(Member member) { return(_members.AddMember(member)); }