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


C# RoleStore类代码示例

本文整理汇总了C#中RoleStore的典型用法代码示例。如果您正苦于以下问题:C# RoleStore类的具体用法?C# RoleStore怎么用?C# RoleStore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: AddUserAndRole

        internal void AddUserAndRole()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

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

            //create admin role
            if(!roleMgr.RoleExists("admin")) {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "admin" });
            }

            //create master user
            UserManager<ApplicationUser> userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            ApplicationUser appUser = new ApplicationUser {
                UserName = "Adam",
                Email = "[email protected]"
            };
            IdUserResult = userMgr.Create(appUser, "Baseball1!");

            //add to admin role
            if(!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "admin")) {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "admin");
            }
        }
开发者ID:adam-currie,项目名称:SET-SQ1-EMS,代码行数:27,代码来源:RoleActions.cs

示例2: Create

        /*This should be removed after the first admin gets made */
        // GET: MakeMeAdmin/Create
        public ActionResult Create(string email)
        {
            using (var context = new ApplicationDbContext())
            {
                var fadmin = context.KeyValueSettings.FirstOrDefault(s => s.Key == "FirstAdminSet");
                if (fadmin == null || fadmin.Value == "false")
                {
                    var roleStore = new RoleStore<IdentityRole>(context);
                    var roleManager = new RoleManager<IdentityRole>(roleStore);

                    roleManager.Create(new IdentityRole("Admin"));

                    var userStore = new UserStore<ApplicationUser>(context);
                    var userManager = new UserManager<ApplicationUser>(userStore);

                    var user = userManager.FindByEmail(email);
                    userManager.AddToRole(user.Id, "Admin");
                    if (fadmin == null)
                    {
                        context.KeyValueSettings.Add(new KeyValueSettings() { Key = "FirstAdminSet", Value = "true" });
                    }
                    else
                    {
                        fadmin.Value = "true";
                    }
                    context.SaveChanges();
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }
开发者ID:emartinez-dat,项目名称:DebesAlgo,代码行数:33,代码来源:MakeMeAdminController.cs

示例3: Menu

        public ActionResult Menu()
        {
            ApplicationDbContext userscontext = new ApplicationDbContext();
            var userStore = new UserStore<ApplicationUser>(userscontext);
            var userManager = new UserManager<ApplicationUser>(userStore);

            var roleStore = new RoleStore<IdentityRole>(userscontext);
            var roleManager = new RoleManager<IdentityRole>(roleStore);

            if (User.Identity.IsAuthenticated)
            {

                if (userManager.IsInRole(this.User.Identity.GetUserId(), "Admin"))
                {
                    return PartialView("_AdminMenuView");
                }
                else if (userManager.IsInRole(this.User.Identity.GetUserId(), "Principal"))
                {
                    return PartialView("_PrincipalenuView");
                }
                else
                {
                    return PartialView("_Student");
                }
            }

            return PartialView("_Empty");
        }
开发者ID:dotnetgeorge,项目名称:SchoolBook,代码行数:28,代码来源:NavigationController.cs

示例4: InitializeAppEnvironment

        private void InitializeAppEnvironment()
        {
            //app environment configuration
            using (var db = new ApplicationDbContext())
            {
                db.Database.CreateIfNotExists();
                var roleStore = new RoleStore<IdentityRole>(db);
                var role = roleStore.FindByNameAsync("Admin").Result;
                if (role == null)
                {
                    roleStore.CreateAsync(new IdentityRole("Admin")).Wait();
                }

                var userStore = new UserStore<ApplicationUser>(db);
                var manager = new ApplicationUserManager(userStore);

                var admin = manager.FindByName("admin");
                if (admin == null)
                {
                    admin = new ApplicationUser
                    {
                        UserName = "admin",
                        Email = "[email protected]fo",
                        EmailConfirmed = true,
                        CreateDate = DateTime.Now
                    };
                    var r = manager.CreateAsync(admin, "~Pwd123456").Result;
                }
                if (!manager.IsInRole(admin.Id, role.Name))
                {
                    manager.AddToRole(admin.Id, role.Name);
                }
            }
        }
开发者ID:kissstudio,项目名称:Topawes,代码行数:34,代码来源:Startup.cs

示例5: gv_RowDataBound

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    DropDownList ddList = (DropDownList)e.Row.FindControl("DropDownList1");

                    Models.ApplicationDbContext context = new ApplicationDbContext();
                    var roleStore = new RoleStore<IdentityRole>(context);

                    var myRoles = new List<string>();

                    var roleMgr = new RoleManager<IdentityRole>(roleStore);
                    foreach (var role in roleMgr.Roles.ToList())
                    {
                        myRoles.Add(role.Name);
                    }
                    ddList.DataSource = myRoles;
                    ddList.DataBind();

                    DataRowView dr = e.Row.DataItem as DataRowView;
                    ddList.SelectedValue = dr["role"].ToString();
                }
            }
        }
开发者ID:dbtdsilva,项目名称:edc-aspnet,代码行数:26,代码来源:ManageRoles.aspx.cs

示例6: 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

示例7: AddUserRole

        public static void AddUserRole(string userName, string roleName)
        {
            using (var context = new ApplicationDbContext())
            {
                try
                {
                    if (!context.Roles.Any(r => r.Name == roleName)) return;

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

                    var store = new UserStore<ApplicationUser>(context);
                    var userManager = new UserManager<ApplicationUser>(store);

                    var user = userManager.FindByName(userName);
                    var role = roleManager.FindByName(roleName);

                    if (userManager.IsInRole(user.Id, role.Name)) return;

                    userManager.AddToRole(user.Id, role.Name);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    // Retrieve the error messages as a list of strings.

                    // ReSharper disable once UnusedVariable
                    var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                    throw;
                }
            }
        }
开发者ID:DanMoyer,项目名称:PageMonitor,代码行数:35,代码来源:UserRoleHelper.cs

示例8: SeedRoles

        public static void SeedRoles()
        {
            var context = new ApplicationDbContext();
            if (!context.Roles.Any())
            {
                if (!context.Roles.Any())
                {
                    var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

                    var roleCreateResult = roleManager.Create(new IdentityRole("Admin"));
                    if (!roleCreateResult.Succeeded)
                    {
                        throw new Exception(string.Join("; ", roleCreateResult.Errors));
                    }

                    var roleStore = new RoleStore<IdentityRole>(context);

                    var userStore = new UserStore<ApplicationUser>(context);

                    var userManager = new UserManager<ApplicationUser>(userStore);
                    var user = new ApplicationUser() { UserName = "[email protected]", Email = "[email protected]" };


                    var createResult = userManager.Create(user, "123456");
                    if (!createResult.Succeeded)
                    {
                        throw new Exception(string.Join("; ", createResult.Errors));
                    }
                    userManager.AddToRole(user.Id, "admin");
                    context.SaveChanges();
                }
            }
        }
开发者ID:veronika793,项目名称:Software-University-Courses,代码行数:33,代码来源:CustomSeed.cs

示例9: AddUserAndRole

        internal void AddUserAndRole()
        {
            Models.ApplicationDbContext context = new Models.ApplicationDbContext();

            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            var roleStore = new RoleStore<IdentityRole>(context);

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

            if (!roleMgr.RoleExists("administrator"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "administrator" });
            }

            var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            var appUser = new ApplicationUser
            {
                UserName = "administrator",
                ImgUrl = "user2-160x160.jpg",
                Description = "High Level",
                SinceDate = new DateTime(2016, 1, 1)
            };

            IdUserResult = userMgr.Create(appUser, "1qaz2wsxE");
            var user = userMgr.FindByName("administrator");
            if (!userMgr.IsInRole(user.Id, "administrator"))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByName("administrator").Id, "administrator");
            }
        }
开发者ID:OurFirstOrgan,项目名称:FlyingSnow.Travel,代码行数:33,代码来源:RoleActions.cs

示例10: Up

        public override void Up()
        {
            ApplicationDbContext context = new ApplicationDbContext();

            var adminUser = new ApplicationUser()
            {
                Id = Guid.NewGuid().ToString(),
                EmailConfirmed = false,
                PhoneNumberConfirmed = false,
                TwoFactorEnabled = false,
                LockoutEnabled = false,
                AccessFailedCount = 0,
                Email = "[email protected]",
                UserName = "[email protected]"
            };

            if (!context.Roles.Any(r => r.Name == "Admin"))
            {
                var store = new RoleStore<IdentityRole>(context);
                var manager = new RoleManager<IdentityRole>(store);
                var role = new IdentityRole { Name = "Admin" };

                manager.Create(role);
            }

            if (!context.Users.Any(u => u.UserName == "TheGaffer"))
            {
                var store = new UserStore<ApplicationUser>(context);
                var manager = new UserManager<ApplicationUser>(store);

                manager.Create(adminUser, "Seisen1!");
                manager.AddToRole(adminUser.Id, "Admin");
            }
        }
开发者ID:RealHerter,项目名称:TheGaffer,代码行数:34,代码来源:201510021948296_test.cs

示例11: AddUserAndRole

        internal void AddUserAndRole()
        {
            Models.ApplicationDbContext db = new ApplicationDbContext();
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            var roleStore = new RoleStore<IdentityRole>(db);

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

            if (!roleMgr.RoleExists("canEdit"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
            }

            var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
            var appUser = new ApplicationUser
            {
                UserName = "[email protected]",
                Email = "[email protected]"
            };
            IdUserResult = userMgr.Create(appUser, "NhatSinh123*");

            if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
            }
        }
开发者ID:VoNhatSinh,项目名称:TestAppHarbor,代码行数:28,代码来源:RoleActions.cs

示例12: Page_Load

 protected void Page_Load(object sender, EventArgs e)
 {
     var roles = new RoleStore<IdentityRole>();
     var rolesManager = new RoleManager<IdentityRole>(roles);
     ddlRol.DataSource = rolesManager.Roles.ToList();
     ddlRol.DataBind();
 }
开发者ID:jennchinchi,项目名称:UAM-p4-Inv,代码行数:7,代码来源:RegistroUsuario.aspx.cs

示例13: GetRoles

        public IQueryable<IdentityRole> GetRoles()
        {
            var roleStore = new RoleStore<IdentityRole>();
            var roles = new RoleManager<IdentityRole>(roleStore);

            return roles.Roles;
        }
开发者ID:M-Yankov,项目名称:UniversityStudentSystem,代码行数:7,代码来源:UserService.cs

示例14: AddUserAndRole

        internal void AddUserAndRole()
        {
            // Access the application context and create result variable.
            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("canEdit"))
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });

            // Create a UserManager object based on the UserStore objcet and the ApplicationDbContext objcet.
            // 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 = "[email protected]",
                Email = "[email protected]"
            };
            IdUserResult = userMgr.Create(appUser, "Pa$$word1");

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

示例15: AddUserAndRole

        internal void AddUserAndRole()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;
            var roleStore = new RoleStore<IdentityRole>(context);
            var roleMgr = new RoleManager<IdentityRole>(roleStore);
            if (!roleMgr.RoleExists("Admin"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "Admin" });
            }

            var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var appUser = new ApplicationUser
            {

                Email = "[email protected]"
            };
            IdUserResult = userMgr.Create(appUser, "adminA123...");

            if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "Admin"))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit");
            }
        }
开发者ID:anitha1993,项目名称:dept-EmpMgmt,代码行数:25,代码来源:RoleActions.cs


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