コード例 #1
0
ファイル: Startup.cs プロジェクト: hlim29/ENET_Care_A2
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);

            var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

            if (!roleManager.RoleExists("manager"))
            {
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "manager";
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("agent"))
            {
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "agent";
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists("doctor"))
            {
                var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                role.Name = "doctor";
                roleManager.Create(role);
            }

            using (var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())))
            using (var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
            {
                var user = um.FindByEmail("*****@*****.**");
                um.AddToRole(user.Id, "agent");

                user = um.FindByEmail("*****@*****.**");
                um.AddToRole(user.Id, "manager");

                user = um.FindByEmail("*****@*****.**");
                um.AddToRole(user.Id, "doctor");
            }
        }
コード例 #2
0
ファイル: Startup.Auth.cs プロジェクト: marduk112/clubmap
        private static void SetupUsers(ApplicationDbContext db)
        {
            using (var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())))
            using (var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
            {
                // Creating roles
                foreach (var role in Enum.GetValues(typeof(Constants.UserRole)))
                {
                    if (rm.RoleExists(role.ToString())) continue;
                    var result = rm.Create(new IdentityRole(role.ToString()));

                    if (!result.Succeeded)
                        throw new ApplicationException("Creating role " + role + " failed with error(s):\n" + GetAllErrors(result));
                }
                // Creating users
                foreach (var newUser in UsersToSetup)
                {
                    var existingUser = um.FindByEmail(newUser.Email);
                    if (existingUser == null)
                    {
                        var result = um.Create(new ApplicationUser
                        {
                            Email = newUser.Email,
                            EmailConfirmed = true,
                            UserName = newUser.Email,
                            LockoutEnabled = newUser.LockoutEnabled
                        },
                        newUser.Password);

                        if (!result.Succeeded)
                            throw new ApplicationException("Creating user " + newUser.Email + " failed with error(s):\n" + GetAllErrors(result));
                    }
                    existingUser = um.FindByEmail(newUser.Email);

                    if (!um.IsInRole(existingUser.Id, Constants.UserRole.Admin.ToString()))
                    {
                        var result = um.AddToRole(existingUser.Id, Constants.UserRole.Admin.ToString());

                        if (!result.Succeeded)
                            throw new ApplicationException("Adding role " + Constants.UserRole.Admin + " for " + newUser.Email + " failed with error(s):\n" + GetAllErrors(result));
                    }
                }
                db.SaveChanges();
            }
        }