Пример #1
0
        public async Task Seed()
        {
            if (await _session.Query<ApplicationUser>().AnyAsync(u => u.UserName == "*****@*****.**"))
                return;

            _session.Store(new IdentityRole { Name = Roles.SystemAdministrator });
            _session.Store(new IdentityRole { Name = Roles.OrganizationAdministrator });
            _session.Store(new IdentityRole { Name = Roles.Therapist });

            _adminUser = new ApplicationUser
            {
                Id = Guid.NewGuid().ToString(),
                Email = "*****@*****.**",
                UserName = "******",
                FirstName = "Stuart",
                LastName = "Clark",
                PhoneNumber = "021509357"
            };

            await _userManager.CreateAsync(_adminUser, "ridgeback");
            await _userManager.AddToRoleAsync(_adminUser.Id, Roles.SystemAdministrator);

            await AddOrganization();
            await AddClients();
            await AddSessions();
            await AddUsers();

            await _session.SaveChangesAsync();
                        
        }
Пример #2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Пример #3
0
 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await user.GenerateUserIdentityAsync(UserManager);// UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
Пример #4
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Пример #5
0
        private async Task AddUsers()
        {
            var organiation = await _session.Query<OrganizationModel>().FirstAsync();

            Func<string, string, string, string, Task> createUser = async (userName, role, firstname, lastname) =>
            {
                if (await  _session.Query<ApplicationUser>().AnyAsync(u => u.UserName == userName) == false)
                {
                    var userToInsert = new ApplicationUser
                    {
                        UserName = userName,
                        FirstName = firstname,
                        LastName = lastname,
                        Email = userName,
                        PhoneNumber = "021509317",
                        OrganizationId = organiation.Id
                    };

                   await _userManager.CreateAsync(userToInsert, "ridgeback");
                   await  _userManager.AddToRoleAsync(userToInsert.Id, role);
                }
            };

            await createUser("*****@*****.**", Roles.Therapist, "Donald", "Trump");
            await createUser("*****@*****.**", Roles.OrganizationAdministrator, "Hilary", "Clinton");
        }