当前位置: 首页>>代码示例>>C#>>正文


C# RoleManager.Create方法代码示例

本文整理汇总了C#中RoleManager.Create方法的典型用法代码示例。如果您正苦于以下问题:C# RoleManager.Create方法的具体用法?C# RoleManager.Create怎么用?C# RoleManager.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RoleManager的用法示例。


在下文中一共展示了RoleManager.Create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitializeRoles

    /// <summary>
    /// Checks for the three roles - Admin, Employee and Complainant and 
    /// creates them if not present
    /// </summary>
    public static void InitializeRoles()
    {  // Access the application context and create result variables.
        ApplicationDbContext context = new ApplicationDbContext();
        IdentityResult IdUserResult;

        // Create a RoleStore object by using the ApplicationDbContext object. 
        // The RoleStore is only allowed to contain IdentityRole objects.
        var roleStore = new RoleStore<IdentityRole>(context);

        RoleManager roleMgr = new RoleManager();
        if (!roleMgr.RoleExists("Administrator"))
        {
            roleMgr.Create(new ApplicationRole { Name = "Administrator" });
        }
        if (!roleMgr.RoleExists("Employee"))
        {
            roleMgr.Create(new ApplicationRole { Name = "Employee" });
        }
        if (!roleMgr.RoleExists("Complainant"))
        {
            roleMgr.Create(new ApplicationRole { Name = "Complainant" });
        }
        if (!roleMgr.RoleExists("Auditor"))
        {
            roleMgr.Create(new ApplicationRole { Name = "Auditor" });
        }
      

        // Create a UserManager object based on the UserStore object and the ApplicationDbContext  
        // object. Note that you can create new objects and use them as parameters in
        // a single line of code, rather than using multiple lines of code, as you did
        // for the RoleManager object.
        var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var appUser = new ApplicationUser
        {
            UserName = "Administrator",
            Email = "[email protected]"
        };
        IdUserResult = userMgr.Create(appUser, "Admin123");

        // If the new "canEdit" user was successfully created, 
        // add the "canEdit" user to the "canEdit" role. 
        if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Administrator"))
        {
            IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Administrator");
        }
         appUser = new ApplicationUser
        {
            UserName = "Auditor",
            Email = "[email protected]"
        };
        IdUserResult = userMgr.Create(appUser, "Auditor123");

        // If the new "canEdit" user was successfully created, 
        // add the "canEdit" user to the "canEdit" role. 
        if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Auditor"))
        {
            IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "Auditor");
        }
    }
开发者ID:chandankpgreen,项目名称:PGRAMS,代码行数:64,代码来源:UserRoleInitialization.cs

示例2: criarRoles

 public void criarRoles()
 {
     var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(ApplicationDbContext.Create()));
     RoleManager.Create(new IdentityRole("Aluno"));
     RoleManager.Create(new IdentityRole("Coordenador"));
     RoleManager.Create(new IdentityRole("Secretario"));
 }
开发者ID:fernandotnl,项目名称:trabalhoASW,代码行数:7,代码来源:UsuariosBusiness.cs

示例3: Users

        public ActionResult Users()
        {
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
            if (!roleManager.RoleExists("user"))
            {
                var nRole = new IdentityRole("user");
                roleManager.Create(nRole);
            }
            if (!roleManager.RoleExists("admin"))
            {
                var nRole = new IdentityRole("admin");
                roleManager.Create(nRole);
            }
            if (!roleManager.RoleExists("moderator"))
            {
                var nRole = new IdentityRole("moderator");
                roleManager.Create(nRole);
            }
            if (!roleManager.RoleExists("journalist"))
            {
                var nRole = new IdentityRole("journalist");
                roleManager.Create(nRole);
            }

            var vm = new AdminUsersViewModel();

            vm.Users = db.Users.OrderBy(x => x.UserName).ToList();
            vm.Roles = db.Roles.ToList();

            return View(vm);
        }
开发者ID:mjdean1994,项目名称:Athenaeum,代码行数:31,代码来源:AdminController.cs

示例4: InitDefaultUsersAndRoles

		private void InitDefaultUsersAndRoles(UserManager<QuestionsAnswersUser> userManager,
			RoleManager<IdentityRole> roleManager)
		{
			if (roleManager.FindByName("admin") == null)
			{
				roleManager.Create(new IdentityRole("admin"));
			}

			if (roleManager.FindByName("user") == null)
			{
				roleManager.Create(new IdentityRole("user"));
			}

			if (userManager.FindByName("admin") == null)
			{
				var user = new QuestionsAnswersUser {UserName = "admin"};
				var result = userManager.Create(user, "adminadmin");
				if (result.Succeeded)
				{
					userManager.AddToRole(user.Id, "admin");
				}
			}

			userManager.Users.Where(u => !u.Roles.Any()).ToList().ForEach(u => userManager.AddToRole(u.Id, "user"));
		}
开发者ID:vitalzhuravkov,项目名称:QuestionsAndAnswers,代码行数:25,代码来源:Global.asax.cs

示例5: SeedRolesAndUsers

        private void SeedRolesAndUsers(ETCCanadaContext context)
        {
            RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(context));

            if (!roleManager.RoleExists("Administrator"))
            {
                roleManager.Create(new ApplicationRole("Administrator"));
            }

            if (!roleManager.RoleExists("User"))
            {
                roleManager.Create(new ApplicationRole("User"));
            }

            List<ApplicationUser> users = new List<ApplicationUser>();
            ApplicationUser user1 = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]", FirstName = "Administrator", LastName = "Administrator", IsApproved = true };
            ApplicationUser user2 = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]", FirstName = "User", LastName = "User", IsApproved = true };
            users.Add(user1);
            users.Add(user2);

            SeedDefaultUser(context, user1, "Administrator");
            SeedDefaultUser(context, user2, "User");

            //http://stackoverflow.com/questions/20470623/asp-net-mvc-asp-net-identity-seeding-roles-and-users
            //http://stackoverflow.com/questions/19280527/mvc5-seed-users-and-roles
            //http://stackoverflow.com/questions/19745286/asp-net-identity-db-seed
        }
开发者ID:majurans,项目名称:ETCCanada,代码行数:27,代码来源:DataSeeder.cs

示例6: Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("BodyTag");
            body.Attributes.Add("class", "login");

            RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>();
            RoleManager<IdentityRole> roleMgr = new RoleManager<IdentityRole>(roleStore);

            if (User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/MainPage.aspx");
            }

            //creating roles//
            if (!roleMgr.RoleExists("Admin"))
            {
                IdentityResult roleResult = roleMgr.Create(new IdentityRole("Admin"));
            }
            if (!roleMgr.RoleExists("Manager"))
            {
                IdentityResult roleResult = roleMgr.Create(new IdentityRole("Manager"));
            }
            if (!roleMgr.RoleExists("SalesAssoc"))
            {
                IdentityResult roleResult = roleMgr.Create(new IdentityRole("SalesAssoc"));
            }
            if (!roleMgr.RoleExists("Designer"))
            {
                IdentityResult roleResult = roleMgr.Create(new IdentityRole("Designer"));
            }
            if (!roleMgr.RoleExists("Laborers"))
            {
                IdentityResult roleResult = roleMgr.Create(new IdentityRole("Laborers"));
            }
        }
开发者ID:amoryjh,项目名称:nbd_asp,代码行数:35,代码来源:Default.aspx.cs

示例7: CreateUserAndRole

        internal void CreateUserAndRole()
        {
            using (var context = new WebDeveloperDbContext())
            {

                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
                var userManager = new UserManager<WebDeveloperUser>(new UserStore<WebDeveloperUser>(context));

                // In Startup iam creating first Admin Role and creating a default Admin User
                if (!roleManager.RoleExists("Admin"))
                {

                    // first we create Admin rool
                    var role = new IdentityRole();
                    role.Name = "Admin";
                    roleManager.Create(role);

                    //Here we create a Admin super user who will maintain the website

                    var user = new WebDeveloperUser
                    {
                        UserName = "[email protected]",
                        Email = "[email protected]"
                    };

                    string userPassword = "Passw0rd2016";

                    var userCreation = userManager.Create(user, userPassword);

                    //Add default User to Role Admin
                    if (userCreation.Succeeded)
                        userManager.AddToRole(user.Id, "Admin");

                }

                // creating Creating Manager role
                if (!roleManager.RoleExists("Manager"))
                {
                    var role = new IdentityRole
                    {
                        Name = "Manager"
                    };
                    roleManager.Create(role);

                }

                // creating Creating Employee role
                if (!roleManager.RoleExists("Employee"))
                {
                    var role = new IdentityRole
                    {
                        Name = "Employee"
                    };
                    roleManager.Create(role);

                }
            }
        }
开发者ID:ChristianUbillus,项目名称:Cibertec_Final_Project,代码行数:58,代码来源:Startup.cs

示例8: SeedApplicaionRole

        private static void SeedApplicaionRole(DbContext dbContext)
        {
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(dbContext));

            roleManager.Create(new IdentityRole {Name = "System Admin"});
            roleManager.Create(new IdentityRole {Name = "Admin"});
            roleManager.Create(new IdentityRole {Name = "Examiner"});
            roleManager.Create(new IdentityRole {Name = "Candidate"});
        }
开发者ID:BrainStation-23,项目名称:recruitment-management-system,代码行数:9,代码来源:SeedData.cs

示例9: SetupRolesAndUsers

        private static void SetupRolesAndUsers(DbContext context)
        {
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            // add roles
            if (!roleManager.RoleExists(Role.Guest.ToString()))
                roleManager.Create(new IdentityRole(Role.Guest.ToString()));
            if (!roleManager.RoleExists(Role.Supplier.ToString()))
                roleManager.Create(new IdentityRole(Role.Supplier.ToString()));
            if (!roleManager.RoleExists(Role.Deactivated.ToString()))
                roleManager.Create(new IdentityRole(Role.Deactivated.ToString()));
            if (!roleManager.RoleExists(Role.User.ToString()))
                roleManager.Create(new IdentityRole(Role.User.ToString()));
            var adminRole = roleManager.FindByName(Role.Admin.ToString());
            if (adminRole == null)
            {
                adminRole = new IdentityRole(Role.Admin.ToString());
                roleManager.Create(adminRole);
            }
            #if DEBUG
            //add admin user
            var admin = userManager.Find(Admin_User, Admin_Pass);
            if (admin == null)
            {
                admin = new ApplicationUser
                {
                    UserName = Admin_User,
                    Email = Admin_Mail,
                    EmailConfirmed = true
                };
                var result = userManager.Create(admin, Admin_Pass);
                // TODO: verify returned IdentityResult
                userManager.AddToRole(admin.Id, Role.Admin.ToString());
                result = userManager.SetLockoutEnabled(admin.Id, false);
            }

            var rolesForUser = userManager.GetRoles(admin.Id);
            if (!rolesForUser.Contains(adminRole.Name))
            {
                var result = userManager.AddToRole(admin.Id, adminRole.Name);
            }

            //add normal user
            if (userManager.Find("[email protected]", "1q2w3e4r") == null)
            {
                var user = new ApplicationUser
                {
                    UserName = "[email protected]",
                    Email = "[email protected]",
                    EmailConfirmed = true
                };
                userManager.Create(user, "1q2w3e4r");
                // TODO: verify returned IdentityResult
                userManager.AddToRole(user.Id, Role.User.ToString());
            }
            #endif
        }
开发者ID:uaandrei,项目名称:d4232ffg,代码行数:57,代码来源:DbConfig.cs

示例10: AddUserAndRole

        internal void AddUserAndRole()
        {
            // Access the application context and create result variables.
            Models.ApplicationDbContext context = new ApplicationDbContext();
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            // Create a RoleStore object by using the ApplicationDbContext object.
            // The RoleStore is only allowed to contain IdentityRole objects.
            var roleStore = new RoleStore<IdentityRole>(context);

            // Create a RoleManager object that is only allowed to contain IdentityRole objects.
            // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object.
            var roleMgr = new RoleManager<IdentityRole>(roleStore);

            // Then, you create the "canEdit" role if it doesn't already exist.
            if (!roleMgr.RoleExists("Admin"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Admin" });
            }

            //create the "canBuy" role if it doesn't already exist.
            if (!roleMgr.RoleExists("Normal"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Normal" });
            }

            // Create a UserManager object based on the UserStore object and the ApplicationDbContext
            // object. Note that you can create new objects and use them as parameters in
            // a single line of code, rather than using multiple lines of code, as you did
            // for the RoleManager object.
            var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var appUser = new ApplicationUser
            {
                UserName = "Michael",
                Email = "[email protected]",
                PhoneNumber = "0172215759"
            };
            IdUserResult = userMgr.Create(appUser, "aaa111A!");

            // If the new "canEdit" user was successfully created,
            // add the "canEdit" user to the "canEdit" role.
            AddToAdminUserRole(appUser);
            AddToNormalUserRole(appUser);
            //if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
            //{
            //    IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
            //}
            //if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canBuy"))
            //{
            //    IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canBuy");
            //}
        }
开发者ID:MhdNZ,项目名称:MenStore,代码行数:53,代码来源:UserRoleActions.cs

示例11: SeedData

        public void SeedData(EmployerEmployeeHuntDbContext context)
        {
            var roleStore = new RoleStore<IdentityRole>(context);
            var roleManager = new RoleManager<IdentityRole>(roleStore);

            var adminRole = new IdentityRole { Name = GlobalConstants.AdministratorRoleName };
            var headhunterRole = new IdentityRole { Name = GlobalConstants.HeadhunterRoleName };
            var userRole = new IdentityRole { Name = GlobalConstants.UserRoleName };
            roleManager.Create(adminRole);
            roleManager.Create(headhunterRole);
            roleManager.Create(userRole);
        }
开发者ID:TsvetanMilanov,项目名称:Employer-Employee-Hunt,代码行数:12,代码来源:RolesDataImporter.cs

示例12: ConfigureRoles

       private void ConfigureRoles(IAppBuilder app)
       {
 
           var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
           if(!roleManager.RoleExists("Administrators"))
           {
               roleManager.Create(new IdentityRole("Administrators"));
               roleManager.Create(new IdentityRole("Users"));
               roleManager.Create(new IdentityRole("ExtendedUsers"));
               roleManager.Create(new IdentityRole("Moderators"));
           }
           
       }
开发者ID:tcns,项目名称:impulse,代码行数:13,代码来源:Startup.Auth.cs

示例13: SeedRoles

        public static void SeedRoles(ApplicationDbContext db)
        {
            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);

            if (!manager.RoleExists(Roles.USER))
            {
                manager.Create(new IdentityRole() { Name = Roles.USER });
            }
            if (!manager.RoleExists(Roles.ADMIN))
            {
                manager.Create(new IdentityRole() { Name = Roles.ADMIN });
            }
        }
开发者ID:giorosati,项目名称:ClassicJaguars,代码行数:14,代码来源:Seeder.cs

示例14: Seed

        public static void Seed(ApplicationDbContext context)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(context);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if (!roleManager.RoleExists("Admin"))
                roleManager.Create(new Role { Name = "Admin" });

            if (!roleManager.RoleExists("User"))
                roleManager.Create(new Role { Name = "User" });

            IdentityResult result = null;

            ApplicationUser user1 = userManager.FindByName("[email protected]");

            if (user1 == null)
            {
                user1 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
            }

            result = userManager.Create(user1, "asdfasdf");
            if (!result.Succeeded)
            {
                string error = result.Errors.FirstOrDefault();
                throw new Exception(error);
            }

            userManager.AddToRole(user1.Id, "Admin");
            user1 = userManager.FindByName("[email protected]");

            ApplicationUser user2 = userManager.FindByName("[email protected]");

            if (user2 == null)
            {
                user2 = new ApplicationUser { Email = "[email protected]", UserName = "[email protected]" };
            }

            result = userManager.Create(user2, "asdfasfd");
            if (!result.Succeeded)
            {
                string error = result.Errors.FirstOrDefault();
                throw new Exception(error);
            }

            userManager.AddToRole(user2.Id, "User");
            user2 = userManager.FindByName("[email protected]");
        }
开发者ID:keigito,项目名称:DryLightningDetector,代码行数:50,代码来源:Seeder.cs

示例15: SeedDbUsers

        public static void SeedDbUsers(ApplicationDbContext context, List<Image> userImages)
        {
            Users = new List<ApplicationUser>();

            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new IdentityRole("Admin"));
            }

            var admin = new ApplicationUser
            {
                UserName = "[email protected]",
                PasswordHash = new PasswordHasher().HashPassword("admin"),
                SecurityStamp = Guid.NewGuid().ToString("D"),
                FirstName = "Admin",
                LastName = "Adminski",
                Email = "[email protected]"
            };

            Admin = admin;
            userManager.Create(admin);
            userManager.AddToRole(admin.Id, "Admin");

            if (!roleManager.RoleExists("User"))
            {
                roleManager.Create(new IdentityRole("User"));
            }

            for (int i = 0; i < 20; i++)
            {
                var user = new ApplicationUser
                {
                    UserName = "user" + i + "@artist.com",
                    PasswordHash = new PasswordHasher().HashPassword("user" + i),
                    SecurityStamp = Guid.NewGuid().ToString("D"),
                    FirstName = "FirstName" + i,
                    LastName = "LastName" + i,
                    Email = "user" + i + "@artist.com",
                    Images = userImages[0]
                };

                userManager.Create(user);
                userManager.AddToRole(admin.Id, "User");
                Users.Add(user);
            }
        }
开发者ID:iliantova,项目名称:MVC-Project-ArtistReview,代码行数:49,代码来源:SeedUsers.cs


注:本文中的RoleManager.Create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。