예제 #1
0
        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"));
        }
예제 #2
0
        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());
        }
예제 #3
0
        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
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    Tuple <string, string> keys = CryptographicHelper.GenerateAsymmetricKeys();

                    _membersService.AddMember(
                        new SecuringApplicationsAssignment.Application.ViewModels.MemberViewModel()
                    {
                        Email     = Input.Email,
                        FirstName = Input.FirstName,
                        LastName  = Input.LastName,
                        //Generating the private key to be

                        PrivateKey = keys.Item2,
                        PublicKey  = keys.Item1
                    });

                    _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());
        }