private async Task BuildSuperUser(IServiceProvider serviceProvider)
        {
            var um = serviceProvider.GetRequiredService <UserManager <GLAAUser> >();

            var su = await um.FindByEmailAsync(Configuration.GetSection("SuperUser")["SuperUserEmail"]);

            if (su == null)
            {
                su = new GLAAUser
                {
                    UserName = Configuration.GetSection("SuperUser")["SuperUserEmail"],
                    Email    = Configuration.GetSection("SuperUser")["SuperUserEmail"]
                };
                var result = await um.CreateAsync(su, Configuration.GetSection("SuperUser")["SuperUserPassword"]);

                if (result.Succeeded)
                {
                    su = await um.FindByEmailAsync(Configuration.GetSection("SuperUser")["SuperUserEmail"]);
                }
                else
                {
                    throw new Exception($"Could not create superuser {result.Errors.First().Description}");
                }
            }

            if (!await um.IsInRoleAsync(su, "Administrator"))
            {
                await um.AddToRoleAsync(su, "Administrator");
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    throw new ApplicationException("Error loading external login information during confirmation.");
                }
                var user = new GLAAUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }
Exemplo n.º 3
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            var user = new GLAAUser
            {
                Id       = "abcd",
                UserName = "******"
            };

            if (User.Identity.IsAuthenticated)
            {
                _authManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
            }

            var id = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);

            id.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", ClaimValueTypes.String));
            id.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName, ClaimValueTypes.String));
            id.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id, ClaimValueTypes.String));

            _authManager.SignIn(new AuthenticationProperties {
                IsPersistent = true
            }, id);

            return(RedirectToLocal(returnUrl));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Register(EligibilitySummaryViewModel model)
        {
            session.Set("LastSubmittedPageSection", "Eligibility");
            session.Set("LastSubmittedPageId", 4);

            if (ModelState.IsValid)
            {
                var user = new GLAAUser
                {
                    UserName = model.Email,
                    Email    = model.Email
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                var licenceId = session.GetCurrentLicenceId();

                if (result.Succeeded)
                {
                    licencePostDataHandler.Update(licenceId, x => x, model);

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

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

                if (result.Errors.Contains($"Email '{model.Email}' is already taken."))
                {
                    model.EmailAlreadyRegistered = true;
                }
                else
                {
                    AddErrors(result);
                }

                licencePostDataHandler.Update(licenceId, x => x, model);
            }

            return(RedirectToAction("Part4", "Eligibility"));
        }
        private static async Task BuildUsers(IServiceProvider serviceProvider)
        {
            var um = serviceProvider.GetRequiredService <UserManager <GLAAUser> >();

            if (um.Users.Count() <= 1)
            {
                var rnd = new Random();

                for (var i = 0; i < 50; i++)
                {
                    string un;
                    string f;
                    string l;
                    do
                    {
                        f  = FirstNames[rnd.Next(FirstNames.Length)];
                        l  = LastNames[rnd.Next(LastNames.Length)];
                        un = $"{f}.{l}@example.com";
                    } while (await um.FindByEmailAsync(un) != null);

                    var user = new GLAAUser
                    {
                        UserName  = un,
                        Email     = un,
                        FirstName = f,
                        LastName  = l
                    };
                    var pw = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

                    var createResult = await um.CreateAsync(user, pw);

                    if (createResult.Succeeded)
                    {
                        user = await um.FindByEmailAsync(un);

                        var availableNames = Roles.Select(r => r.Name).ToArray();
                        var role           = availableNames[rnd.Next(availableNames.Length)];

                        await um.AddToRoleAsync(user, role);
                    }
                }
            }
        }
        private async Task <IEnumerable <GLAAUser> > BuildUsersFromSectionWithRoleAsync(IServiceProvider serviceProvider, string section, string role)
        {
            var userManager = serviceProvider.GetRequiredService <UserManager <GLAAUser> >();

            var keyValuePairs = Configuration.GetSection(section)
                                .AsEnumerable()
                                .Where(x => x.Key.Contains(section) &&
                                       x.Key.Contains("Email") &&
                                       !string.IsNullOrEmpty(x.Value))
                                .ToList();

            var users = new List <GLAAUser>();

            for (var i = 0; i < keyValuePairs.Count; i++)
            {
                var user = new GLAAUser();

                Configuration.GetSection($"{section}:{i}").Bind(user);
                var password = Configuration.GetSection($"{section}:{i}:Password").Value;
                var email    = Configuration.GetSection($"{section}:{i}:Email").Value;

                user.UserName = email;

                users.Add(user);

                var result = await userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    if (!await userManager.IsInRoleAsync(user, role))
                    {
                        await userManager.AddToRoleAsync(user, role);
                    }
                }
            }

            return(users);
        }
Exemplo n.º 7
0
        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 _authManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new GLAAUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }