public async Task<bool> CreateAsync(UserViewModel model)
        {
            if (!_modelState.IsValid)
            {
                return false;
            }

            var existingUser = await _users.GetUserByNameAsync(model.UserName);

            if (existingUser != null)
            {
                _modelState.AddModelError(string.Empty, "The user already exists!");
                return false;
            }

            if (string.IsNullOrWhiteSpace(model.NewPassword))
            {
                _modelState.AddModelError(string.Empty, "You must type a password.");
                return false;
            }

            var newUser = new CmsUser
            {
                DisplayName = model.DisplayName,
                Email = model.Email,
                UserName = model.UserName
            };

            await _users.CreateAsync(newUser, model.NewPassword);

            await _users.AddUserToRoleAsync(newUser, model.SelectedRole);

            return true;
        }
        public static void RegisterAdmin()
        {
            using (var context = new CmsContext())
            using (var userStore = new UserStore<CmsUser>(context))
            using (var userManager = new UserManager<CmsUser>(userStore))
            {
                var user = userStore.FindByNameAsync("admin").Result;

                if (user == null)
                {
                    var adminUser = new CmsUser
                    {
                        UserName = "******",
                        Email = "*****@*****.**",
                        DisplayName = "Administrator"
                    };

                    userManager.Create(adminUser, "Passw0rd1234");
                }
            }
        }
Ejemplo n.º 3
0
        public async Task<ActionResult> Register(UserViewModel model)
        {
            var user = new CmsUser { UserName = model.Email, Email = model.Email };

            var completed = await _users.CreateAsync(model);

            if (completed)
            {

                //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                //created user
                //log them in
                //var authManager = HttpContext.Current.GetOwinContext().Authentication;
                //var userIdentity - CmsUserManager.CreateIdentity()
                //    authManager.SignIn(userIdentity);
                //Response.Redirect("");

                return RedirectToAction("index",  "admin");
            }

            return View(model);
        }
        public async static Task RegisterAdmin()
        {
            using (var users = new UserRepository())
            {
                var user = await users.GetUserByNameAsync("admin");

                if (user == null)
                {
                    var adminUser = new CmsUser
                    {
                        UserName = "******",
                        Email = "*****@*****.**",
                        DisplayName = "Administrator"
                    };

                    await users.CreateAsync(adminUser, "Passw0rd1234");
                }
            }

            using (var roles = new RoleRepository())
            {
                if (await roles.GetRoleByNameAsync("admin") == null)
                {
                    await roles.CreateAsync(new IdentityRole("admin"));
                }

                if (await roles.GetRoleByNameAsync("editor") == null)
                {
                    await roles.CreateAsync(new IdentityRole("editor"));
                }

                if (await roles.GetRoleByNameAsync("author") == null)
                {
                    await roles.CreateAsync(new IdentityRole("author"));
                }
            }
        }
        public async Task<bool> Create(UserViewModel model)
        {
            if (!_modelState.IsValid)
            {
                return false;
            }

            if (string.IsNullOrWhiteSpace(model.NewPassword))
            {
                _modelState.AddModelError(string.Empty, "You must type a password.");
                return false;
            }

            var newUser = new CmsUser
            {
                DisplayName = model.DisplayName,
                Email = model.Email,
                UserName = model.UserName
            };

            await _users.CreateAsync(newUser, model.NewPassword);

            return true;
        }
 public async Task UpdateAsync(CmsUser user)
 {
     await _manager.UpdateAsync(user);
 }
 public async Task DeleteAsync(CmsUser user)
 {
     await _manager.DeleteAsync(user);
 }
 public async Task CreateAsync(CmsUser user, string password)
 {
     await _manager.CreateAsync(user, password);
 }
 public async Task<ClaimsIdentity> CreateIdentityAsync(CmsUser user)
 {
     return await _manager.CreateIdentityAsync(
         user, DefaultAuthenticationTypes.ApplicationCookie);
 }
 public async Task RemoveUserFromRoleAsync(CmsUser user, params string[] roleNames)
 {
     await _manager.RemoveFromRolesAsync(user.Id, roleNames);
 }
 public async Task<IEnumerable<string>> GetRolesForUserAsync(CmsUser user)
 {
     return await _manager.GetRolesAsync(user.Id);
 }
 public async Task AddUserToRoleAsync(CmsUser user, string role)
 {
     await _manager.AddToRoleAsync(user.Id, role);
 }
 public void Update(CmsUser user)
 {
     var result = _manager.UpdateAsync(user).Result;
 }
 public void Delete(CmsUser user)
 {
     var result = _manager.DeleteAsync(user).Result;
 }
Ejemplo n.º 15
0
        public async static Task RegisterAdmin()
        {
            /*
            using (var posts = new PostRepository())
            {
                //var post = await posts.GetAllAsync();

                if( post == null)
                {
                    var defaultPost = new Post
                    {
                        Title = "Title"
                        //fields here
                    }

                   await posts.Create(defaultPost);
                }
            }
            */

            using (var users = new UserRepository())
            {
                var user = await users.GetUserByNameAsync("admin");

                if (user == null)
                {
                    var adminUser = new CmsUser
                    {
                        UserName = "******",
                        Email = "*****@*****.**",
                        DisplayName = "*****@*****.**",
                        FirstName = "Application",
                        LastName = "Admin"
                    };

                    //sett a default password to AppAdmin
                    await users.CreateAsync(adminUser, "Passw0rd1234");
                    //adding AppAdmin to "admin" Role
                    await users.AddUserToRoleAsync(adminUser, "admin");
                }
            }

            //List All Possible Roles
            using (var roles = new RoleRepository())
            {
                if (await roles.GetRoleByNameAsync("admin") == null)
                {
                    await roles.CreateAsync(new IdentityRole("admin"));
                }

                if (await roles.GetRoleByNameAsync("editor") == null)
                {
                    await roles.CreateAsync(new IdentityRole("editor"));
                }

                if (await roles.GetRoleByNameAsync("author") == null)
                {
                    await roles.CreateAsync(new IdentityRole("author"));
                }

                if (await roles.GetRoleByNameAsync("client") == null)
                {
                    await roles.CreateAsync(new IdentityRole("client"));
                }

            }

        }