コード例 #1
0
        public async Task <IActionResult> Edit(string id, string Roles, [Bind("Firstname,LastName,Street,City,Province,PostalCode,Country,MobileNumber,SailingExperience,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] ApplicationUser applicationUser)
        {
            if (id != applicationUser.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (Roles != null)
                    {
                        var    roleList = _context.Roles.ToList();
                        String roleId   = "";
                        foreach (var role in roleList)
                        {
                            if (role.Name == Roles)
                            {
                                roleId = role.Id;
                                break;
                            }
                        }

                        _context.Update(applicationUser);
                        var userAndRole = new Microsoft.AspNetCore.Identity.IdentityUserRole <string>();
                        userAndRole.UserId = id;
                        userAndRole.RoleId = roleId;

                        if (_context.UserRoles.Find(id, roleId) != null)
                        {
                        }
                        else
                        {
                            _context.UserRoles.Add(userAndRole);
                        }
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ApplicationUserExists(applicationUser.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(applicationUser));
        }
コード例 #2
0
        private static void AddTestData(CrivDbContext context, IConfiguration _config)
        {
            var testuser = _config.GetSection("TestUser");
            var user     = new CrivServer.Data.Models.ApplicationUser {
                Id                 = "1",
                UserName           = testuser.GetValue <string>("UserName"),
                Email              = testuser.GetValue <string>("Email"),
                NormalizedUserName = testuser.GetValue <string>("NormalizedUserName"),
                NormalizedEmail    = testuser.GetValue <string>("NormalizedEmail"),
                PhoneNumber        = testuser.GetValue <string>("PhoneNumber"),
                PasswordHash       = testuser.GetValue <string>("PasswordHash"),
                UserType           = testuser.GetValue <int>("UserType"),
                UserFolder         = Guid.NewGuid().ToString(),
                SecurityStamp      = Guid.NewGuid().ToString()
            };

            context.ApplicationUsers.Add(user);

            var role = new Microsoft.AspNetCore.Identity.IdentityRole()
            {
                Id = "1", Name = "Creator", NormalizedName = "creator", ConcurrencyStamp = Guid.NewGuid().ToString()
            };

            context.Roles.Add(role);

            var userrole = new Microsoft.AspNetCore.Identity.IdentityUserRole <string>()
            {
                RoleId = "1", UserId = "1"
            };

            context.UserRoles.Add(userrole);

            var page = new CrivServer.Data.Models.DbContentModel {
                content_id         = 1,
                site_id            = 1,
                url                = "test",
                tab_title          = "Test page",
                canonical          = "",
                page_title         = "Test page title",
                meta_description   = "A test page description",
                content_h1         = "h1 test 1",
                main_content       = "A main content on a test page",
                additional_content = "Additional test content",
                layout             = "",
                published_date     = new System.DateTime(),
                content_type       = 0,
                status             = 1,
                redirect_url       = "",
                auth_level         = null
            };

            context.ContentModels.Add(page);

            var page2 = new CrivServer.Data.Models.DbContentModel
            {
                content_id         = 2,
                site_id            = 1,
                url                = "test-2",
                tab_title          = "Test page 2",
                canonical          = "",
                page_title         = "Test page2 title",
                meta_description   = "A test page2 description",
                content_h1         = "h1 test 2",
                main_content       = "A main content on a test page2",
                additional_content = "Additional test content",
                layout             = "",
                published_date     = new System.DateTime(),
                content_type       = 0,
                status             = 1,
                redirect_url       = "",
                auth_level         = 0
            };

            context.ContentModels.Add(page2);

            var aboutpage = new CrivServer.Data.Models.DbContentModel
            {
                content_id         = 3,
                site_id            = 1,
                url                = "about",
                tab_title          = "About page",
                canonical          = "",
                page_title         = "About page title",
                meta_description   = "An about page description",
                content_h1         = "h1 about page",
                main_content       = "A main content on the about page",
                additional_content = "Additional about content",
                layout             = "",
                published_date     = new System.DateTime(),
                content_type       = 0,
                status             = 1,
                redirect_url       = "",
                auth_level         = 1
            };

            context.ContentModels.Add(aboutpage);

            var creationpage = new CrivServer.Data.Models.DbContentModel
            {
                content_id         = 4,
                site_id            = 1,
                url                = "creation",
                tab_title          = "creation page",
                canonical          = "",
                page_title         = "creation page title",
                meta_description   = "An creation page description",
                content_h1         = "h1 creation page",
                main_content       = "A main content on the creation page",
                additional_content = "Additional creation content",
                layout             = "",
                published_date     = new System.DateTime(),
                content_type       = 0,
                status             = 1,
                redirect_url       = "",
                auth_level         = 1
            };

            context.ContentModels.Add(creationpage);

            var randompage = new CrivServer.Data.Models.DbContentModel
            {
                content_id         = 5,
                site_id            = 1,
                url                = "random",
                tab_title          = "random page",
                canonical          = "",
                page_title         = "random page title",
                meta_description   = "An random page description",
                content_h1         = "h1 random page",
                main_content       = "A main content on the random page",
                additional_content = "Additional random content",
                layout             = "",
                published_date     = new System.DateTime(),
                content_type       = 0,
                status             = 1,
                redirect_url       = "",
                auth_level         = 1
            };

            context.ContentModels.Add(randompage);
            context.SaveChanges();
        }