Ejemplo n.º 1
0
        public AppRole CreateOrFindRole(AppRoleEnum roleEnum)
        {
            var role = database.Roles.SingleOrDefault(r => r.RoleEnum == roleEnum);

            if (role == null)
            {
                lock (roleLocker)
                {
                    role = database.Roles.SingleOrDefault(r => r.RoleEnum == roleEnum);
                    if (role == null)
                    {
                        var newRole = new AppRole
                        {
                            Name     = roleEnum.ToString(),
                            RoleEnum = roleEnum
                        };
                        database.Roles.Add(newRole);
                        Save();
                        role = database.Roles.SingleOrDefault(r => r.RoleEnum == roleEnum);
                    }
                }
            }
            return(role);
        }
Ejemplo n.º 2
0
        public IdentityResult AddToRole(int userId, AppRoleEnum roleEnum)
        {
            var role = CreateOrFindRole(roleEnum);

            return(AddToRole(userId: userId, roleName: roleEnum.ToString()));
        }