Esempio n. 1
0
        private User LoginUser(User user)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim("FirstName", user.FirstName),
                new Claim("LastName", user.LastName)
            };

            foreach (var role in user.InverseThis2Roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role.This2RoleNavigation.Key));
            }

            foreach (var userGroups in DbContext.User2Groups.Where(a => a.This2User == user.ObjId))
            {
                var roles = DbContext.UserGroup2Roles.Where(a => a.This2UserGroup == userGroups.This2UserGroup)
                            .Include(a => a.This2RoleNavigation);

                foreach (var userGroupRoles in roles)
                {
                    claims.Add(new Claim(ClaimTypes.Role, userGroupRoles.This2RoleNavigation.Key));

                    user.InverseThis2Roles.Add(new User2Role()
                    {
                        This2User           = user.ObjId,
                        This2Role           = userGroupRoles.This2Role,
                        This2RoleNavigation = userGroupRoles.This2RoleNavigation
                    });
                }

                claims.Add(new Claim(UserGroup.ClaimType, userGroups.This2UserGroup.ToString()));
            }

            var claimsIdentity = new ClaimsIdentity(claims);

            var authProperties = new AuthenticationProperties
            {
                IsPersistent = true
            };
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = ServerInfo.ServerUid.ToByteArray();
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = claimsIdentity,
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key),
                                                            SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            user.Token = tokenHandler.WriteToken(token);

            return(user);
        }
Esempio n. 2
0
        private static void AddInitUserManagementData(AutomaticaContext context)
        {
            var salt   = UserHelper.GenerateNewSalt();
            var saUser = new User
            {
                UserName  = "******",
                FirstName = "admin",
                LastName  = "admin",
                Salt      = salt,
                Password  = UserHelper.HashPassword("sa", salt),
                ObjId     = Guid.NewGuid()
            };

            context.Add(saUser);


            var adminGroup = new UserGroup
            {
                ObjId = Guid.NewGuid(),
                Name  = "Admin"
            };

            context.Add(adminGroup);


            var adminRole = new Role
            {
                ObjId       = Guid.NewGuid(),
                Name        = "ROLES.ADMINISTRATOR.NAME",
                Description = "ROLES.ADMINISTRATOR.DESCRIPTION",
                Key         = Role.AdminRole
            };

            context.Add(adminRole);

            var priviliedge = new Priviledge
            {
                ObjId       = Guid.NewGuid(),
                Name        = "PRIVILEDGE.ALL.NAME",
                Description = "PRIVILEDGE.ALL.DESCRIPTION",
                Key         = "superuser"
            };

            context.Add(priviliedge);


            context.Add(new Priviledge2Role
            {
                This2Priviledge = priviliedge.ObjId,
                This2Role       = adminRole.ObjId
            });

            context.Add(new User2Group
            {
                This2User      = saUser.ObjId,
                This2UserGroup = adminGroup.ObjId
            });

            context.Add(new User2Role
            {
                This2User = saUser.ObjId,
                This2Role = adminRole.ObjId
            });


            salt = UserHelper.GenerateNewSalt();
            var visuUser = new User
            {
                UserName  = "******",
                FirstName = "Visu",
                LastName  = "Visu",
                Salt      = salt,
                Password  = UserHelper.HashPassword("visu", salt),
                ObjId     = Guid.NewGuid()
            };

            context.Add(visuUser);

            var visuGroup = new UserGroup
            {
                ObjId = Guid.NewGuid(),
                Name  = "Visu"
            };

            context.Add(visuGroup);

            context.Add(new User2Group
            {
                This2User      = visuUser.ObjId,
                This2UserGroup = visuGroup.ObjId
            });


            var visuRole = new Role
            {
                ObjId       = Guid.NewGuid(),
                Name        = "ROLES.VISUALISATION.NAME",
                Description = "ROLES.VISUALISATION.DESCRIPTION",
                Key         = Role.VisuRole
            };

            context.Add(visuRole);

            var visuPriviliedge = new Priviledge
            {
                ObjId       = Guid.NewGuid(),
                Name        = "PRIVILEDGE.VISU.NAME",
                Description = "PRIVILEDGE.VISU.DESCRIPTION",
                Key         = "visu"
            };

            context.Add(visuPriviliedge);


            context.Add(new Priviledge2Role
            {
                This2Priviledge = visuPriviliedge.ObjId,
                This2Role       = visuRole.ObjId
            });

            context.Add(new User2Role
            {
                This2User = visuUser.ObjId,
                This2Role = visuRole.ObjId
            });


            context.Add(new UserGroup2Role()
            {
                This2UserGroup = visuGroup.ObjId,
                This2Role      = visuRole.ObjId
            });


            context.SaveChanges();
        }