コード例 #1
0
        private async Task InitIdentityAsync(IServiceScope scope)
        {
            RoleManager <WebAppRole> roleManager = scope.ServiceProvider.GetRequiredService <RoleManager <WebAppRole> >();
            UserManager <WebAppUser> userManager = scope.ServiceProvider.GetRequiredService <UserManager <WebAppUser> >();

            bool exists = await roleManager.Roles.Where(x => x.NormalizedName == "ADMIN").AnyAsync();

            if (exists)
            {
                return;
            }

            var roleAdmin = new WebAppRole
            {
                Name           = "Admin",
                NormalizedName = "ADMIN",
                Title          = "Administrator",
                Seq            = 30
            };

            var roleUser = new WebAppRole
            {
                Name           = "User",
                NormalizedName = "USER",
                Title          = "User",
                Seq            = 20
            };

            var roleGuest = new WebAppRole
            {
                Name           = "Guest",
                NormalizedName = "GUEST",
                Title          = "Guest",
                Seq            = 10
            };

            await roleManager.CreateAsync(roleAdmin);

            await roleManager.CreateAsync(roleUser);

            await roleManager.CreateAsync(roleGuest);

            WebAppUser webAppUser = new WebAppUser
            {
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                Title          = "Administrator",
                UserName       = "******",
            };

            await userManager.CreateAsync(webAppUser, "Admin&890");

            await userManager.AddToRoleAsync(webAppUser, "Admin");
        }
コード例 #2
0
        public async Task <IActionResult> OnPostAdminProfileAsync()
        {
            IFormCollection requestForm = await Request.ReadFormAsync();

            var username = requestForm["UserName"];

            if (username.Count == 0)
            {
                return(BadRequest());
            }

            var user = await _userManager.FindByNameAsync(username);

            if (user == null)
            {
                return(NotFound());
            }

            string     email    = requestForm["Input.Email"];
            string     title    = requestForm["Input.Title"];
            string     phone    = requestForm["Input.PhoneNumber"];
            string     roleId   = requestForm["Input.Role"];
            WebAppRole roleUser = await _roleManager.FindByIdAsync(roleId);

            string[] formConfirm = Request.Form["Input.IsConfirm"];
            bool     isConfirm   = false;

            if (formConfirm.FirstOrDefault() != null)
            {
                isConfirm = bool.Parse(formConfirm.FirstOrDefault());
            }

            if (user.Email != email)
            {
                user.Email          = email;
                user.EmailConfirmed = false;
            }

            if (user.Title != title)
            {
                user.Title = title;
            }

            if (user.PhoneNumber != phone)
            {
                user.PhoneNumber = phone;
            }

            if (isConfirm)
            {
                user.EmailConfirmed = true;
            }

            await _userManager.UpdateAsync(user);

            IList <string> roles = await _userManager.GetRolesAsync(user);

            await _userManager.RemoveFromRolesAsync(user, roles);

            await _userManager.AddToRoleAsync(user, roleUser.Name);

            return(Ok());
        }
コード例 #3
0
ファイル: Startup.cs プロジェクト: lulzzz/OrderMaker
        private void InitDataBase(IServiceProvider serviceProvider)
        {
            var         roleManager  = serviceProvider.GetRequiredService <RoleManager <WebAppRole> >();
            Task <bool> hasAdminRole = roleManager.RoleExistsAsync("Admin");

            hasAdminRole.Wait();

            if (!hasAdminRole.Result)
            {
                var roleAdmin = new WebAppRole
                {
                    Name           = "Admin",
                    NormalizedName = "ADMIN",
                    Title          = "Administrator",
                    Seq            = 30
                };

                var roleUser = new WebAppRole
                {
                    Name           = "User",
                    NormalizedName = "USER",
                    Title          = "User",
                    Seq            = 20
                };

                var roleGuest = new WebAppRole
                {
                    Name           = "Guest",
                    NormalizedName = "GUEST",
                    Title          = "Guest",
                    Seq            = 10
                };

                roleManager.CreateAsync(roleAdmin).Wait();
                roleManager.CreateAsync(roleUser).Wait();
                roleManager.CreateAsync(roleGuest).Wait();
            }

            var         userManager = serviceProvider.GetRequiredService <UserManager <WebAppUser> >();
            Task <bool> hasUser     = userManager.Users.AnyAsync();

            hasUser.Wait();

            if (!hasUser.Result)
            {
                var config = serviceProvider.GetRequiredService <IOptions <ConfigSettings> >();

                WebAppUser webAppUser = new WebAppUser
                {
                    Email          = config.Value.EmailSupport,
                    EmailConfirmed = true,
                    Title          = "Administrator",
                    UserName       = config.Value.DefaultUSR,
                };

                userManager.CreateAsync(webAppUser, config.Value.DefaultPWD).Wait();
                userManager.AddToRoleAsync(webAppUser, "Admin").Wait();
            }

            var context = serviceProvider.GetRequiredService <OrderMakerContext>();

            Task <bool> formGroup = context.MtdCategoryForm.AnyAsync();

            formGroup.Wait();

            if (!formGroup.Result)
            {
                MtdCategoryForm mtdGroupForm = new MtdCategoryForm
                {
                    Id          = "17101180-9250-4498-BE4E-4A941AD6713C",
                    Name        = "Default",
                    Description = "Default Group",
                    Parent      = "17101180-9250-4498-BE4E-4A941AD6713C"
                };

                context.MtdCategoryForm.Add(mtdGroupForm);
                context.SaveChanges();
            }

            Task <bool> sysType = context.MtdSysType.AnyAsync();

            sysType.Wait();

            if (!sysType.Result)
            {
                List <MtdSysType> mtdSysTypes = new List <MtdSysType> {
                    new MtdSysType {
                        Id = 1, Name = "Text", Description = "Text", Active = true
                    },
                    new MtdSysType {
                        Id = 2, Name = "Integer", Description = "Integer", Active = true
                    },
                    new MtdSysType {
                        Id = 3, Name = "Decimal", Description = "Decimal", Active = true
                    },
                    new MtdSysType {
                        Id = 4, Name = "Memo", Description = "Memo", Active = true
                    },
                    new MtdSysType {
                        Id = 5, Name = "Date", Description = "Date", Active = true
                    },
                    new MtdSysType {
                        Id = 6, Name = "DateTime", Description = "DateTime", Active = true
                    },
                    new MtdSysType {
                        Id = 7, Name = "File", Description = "File", Active = true
                    },
                    new MtdSysType {
                        Id = 8, Name = "Image", Description = "Image", Active = true
                    },
                    //new MtdSysType{ Id = 9, Name="Phone",Description="Phone",Active=false},
                    //new MtdSysType{ Id = 10, Name="Time",Description="Time",Active=false},
                    new MtdSysType {
                        Id = 11, Name = "List", Description = "List", Active = true
                    },
                    new MtdSysType {
                        Id = 12, Name = "Checkbox", Description = "Checkbox", Active = true
                    },
                };

                context.MtdSysType.AddRange(mtdSysTypes);
                context.SaveChanges();
            }

            Task <bool> sysTerm = context.MtdSysTerm.AnyAsync();

            sysTerm.Wait();
            if (!sysTerm.Result)
            {
                List <MtdSysTerm> mtdSysTerms = new List <MtdSysTerm>
                {
                    new MtdSysTerm {
                        Id = 1, Name = "equal", Sign = "="
                    },
                    new MtdSysTerm {
                        Id = 2, Name = "less", Sign = "<"
                    },
                    new MtdSysTerm {
                        Id = 3, Name = "more", Sign = ">"
                    },
                    new MtdSysTerm {
                        Id = 4, Name = "contains", Sign = "~"
                    },
                    new MtdSysTerm {
                        Id = 5, Name = "no equal", Sign = "<>"
                    },
                };

                context.MtdSysTerm.AddRange(mtdSysTerms);
                context.SaveChanges();
            }

            Task <bool> sysStyle = context.MtdSysStyle.AnyAsync();

            sysStyle.Wait();
            if (!sysStyle.Result)
            {
                List <MtdSysStyle> mtdSysStyles = new List <MtdSysStyle>
                {
                    new MtdSysStyle {
                        Id = 4, Name = "Line", Description = "Line", Active = true
                    },
                    new MtdSysStyle {
                        Id = 5, Name = "Column", Description = "Column", Active = true
                    }
                };

                context.MtdSysStyle.AddRange(mtdSysStyles);
                context.SaveChanges();
            }
        }