Exemple #1
0
        public async Task <IActionResult> outgoing(UsersVM model)
        {
            bool x = await _roleManager.RoleExistsAsync("OutgoingApplicant");

            if (!x)
            {
                await _roleManager.CreateAsync(new IdentityRole
                {
                    Name = "OutgoingApplicant"
                });
            }

            //password must be strong enough in order for userManager.CreateAsync to work!!!
            string password = "******";

            var brojKorisnika = _db.Users.Count();

            brojac = ++brojKorisnika;

            ApplicationUser user = new ApplicationUser
            {
                Name        = model.Name,
                Surname     = model.Surname,
                Email       = model.Email,
                PhoneNumber = model.PhoneNumber,
                CountryId   = model.CountryId,
                UserName    = model.Name.ToLower() + '.' + model.Surname.ToLower(),
                UniqueCode  = GetRandomizedString(brojac),
            };

            await _userManager.CreateAsync(user, password);

            await _userManager.AddToRoleAsync(user, "OutgoingApplicant");

            IRO_UNMO.App.Models.Applicant applicant = new IRO_UNMO.App.Models.Applicant
            {
                ApplicantId       = user.Id,
                CreatedProfile    = DateTime.Now,
                UniversityId      = 23,
                FacultyName       = model.FacultyName,
                TypeOfApplication = model.TypeOfApplication,
                StudyCycle        = model.StudyCycle,
                StudyField        = model.StudyField,
                Verified          = false
            };

            _db.Applicant.Add(applicant);
            _db.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
Exemple #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                //bool x = await _roleManager.RoleExistsAsync("IncomingApplicant");

                //if (!x)
                //{
                //    await _roleManager.CreateAsync(new IdentityRole
                //    {
                //        Name = "IncomingApplicant"
                //    });
                //}

                //password must be strong enough in order for userManager.CreateAsync to work!!!
                string password = "******";

                var brojKorisnika = _db.Users.Count();

                //brojac = ++brojKorisnika;

                ApplicationUser user = new ApplicationUser
                {
                    Name        = Input.Name,
                    Surname     = Input.Surname,
                    Email       = Input.Email,
                    PhoneNumber = Input.PhoneNumber,
                    CountryId   = Input.NationalityId,
                    UserName    = Input.Name.ToLower() + '.' + Input.Surname.ToLower(),
                    UniqueCode  = "xxxx", //GetRandomizedString(brojac),
                    LastLogin   = DateTime.Now
                };

                await _userManager.CreateAsync(user, password);

                await _userManager.AddToRoleAsync(user, "IncomingApplicant");

                IRO_UNMO.App.Models.Applicant a = new IRO_UNMO.App.Models.Applicant
                {
                    ApplicantId       = user.Id,
                    CreatedProfile    = DateTime.Now,
                    UniversityId      = Input.UniversityId,
                    StudyCycle        = Input.StudyCycle,
                    StudyField        = Input.StudyField,
                    Verified          = false,
                    TypeOfApplication = Input.ApplicantType
                };

                _db.Applicant.Add(a);
                _db.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }

            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };
                IdentityResult result = null;
                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, 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>.");

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