コード例 #1
0
 /// <summary>
 /// Create role
 /// </summary>
 /// <param name="myRole">the role to add</param>
 public void CreateRole(string myRole)
 {
     if (!MyRoleManager.RoleExists(myRole))
     {
         MyRoleManager.Create(new IdentityRole(myRole));
     }
 }
コード例 #2
0
 /// <summary>
 /// Create roles
 /// </summary>
 /// <param name="roleName">a string represetning the name of the role to be created</param>
 public void createRole(string roleName)
 {
     //check if the role exists
     if (!MyRoleManager.RoleExists(roleName))
     {
         //create role
         CreateRole(roleName);
     }
 }
コード例 #3
0
 /// <summary>
 /// Create roles
 /// </summary>
 /// <param name="roleName">a string represetning the name of the role to be created</param>
 public void removeRole(string roleName)
 {
     //check if the role exists
     if (MyRoleManager.RoleExists(roleName))
     {
         var role = m_idbctx.Roles.Where(r => r.Name == roleName).FirstOrDefault();
         m_idbctx.Roles.Remove(role);
         m_idbctx.SaveChanges();
     }
     else
     {
         HttpContext.Current.Session["Error"] = roleName + " does not exist";
         HttpContext.Current.Response.Redirect("~/UserPages/ErrorPage.aspx");
     }
 }
コード例 #4
0
        protected override void Seed(Infrastructure.DbContext.MyIdentityDbContext context)
        {
            MyUserManager userManager = new MyUserManager(new UserStore <MyUser>(context));
            MyRoleManager roleManager = new MyRoleManager(new RoleStore <MyRole>(context));

            string roleName = "Administrator";
            string userName = "******";
            string password = "******";
            string email    = "*****@*****.**";

            if (!roleManager.RoleExists(roleName))
            {
                roleManager.Create(new MyRole(roleName));
            }

            MyUser user = userManager.FindByName(userName);

            if (user == null)
            {
                userManager.Create(new MyUser {
                    UserName = userName, Email = email
                },
                                   password);
                user = userManager.FindByName(userName);
            }

            if (!userManager.IsInRole(user.Id, roleName))
            {
                userManager.AddToRole(user.Id, roleName);
            }
            foreach (MyUser dbUser in userManager.Users)
            {
                if (dbUser.Country == Countries.None)
                {
                    dbUser.SetCountryFromCity(dbUser.City);
                }
            }
            context.SaveChanges();
        }