Ejemplo n.º 1
0
        public ActionResult AppUsers_Destroy([DataSourceRequest]DataSourceRequest request, AppUser appUser)
        {
            this.users.Remove(appUser);
            this.users.SaveChanges();

            return this.Json(new[] { appUser }.ToDataSourceResult(request, this.ModelState));
        }
Ejemplo n.º 2
0
        public ActionResult AppUsers_Create([DataSourceRequest]DataSourceRequest request, AppUserGridViewModel appUser)
        {
            var userManager = new UserManager<AppUser>(new UserStore<AppUser>(this.context));
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this.context));

            if (!roleManager.Roles.Any())
            {
                roleManager.Create(new IdentityRole { Name = "User" });
                roleManager.Create(new IdentityRole { Name = "Admin" });
            }

            var newId = string.Empty;
            if (this.ModelState.IsValid)
            {
                AppUser userToCreate = new AppUser
                {
                    Avatar = appUser.Avatar,
                    Email = appUser.Email,
                    UserName = appUser.UserName,
                    FirstName = appUser.FirstName,
                    LastName = appUser.LastName
                };

                userManager.Create(userToCreate, appUser.Password);
                userManager.AddToRole(userToCreate.Id, "User");
                newId = userToCreate.Id;
            }

            var userToDisplay =
             this.users.All()
             .To<AppUserGridViewModel>()
             .FirstOrDefault(x => x.Id == newId);

            return this.Json(new[]
            {
                userToDisplay
            }.ToDataSourceResult(request, this.ModelState));
        }