Esempio n. 1
0
        public async Task <bool> CreateUserIfNotExists(string username, string password, params string[] permissions)
        {
            var exists = await _securityRepository.Exists <ApplicationUser>(u => u.UserName == username);

            if (exists)
            {
                Log(true, nameof(ApplicationUser), username);
            }
            else
            {
                Log(false, nameof(ApplicationUser), username);
                var user = CreateUser(username, password, false);
                await _securityRepository.AddUser(user);

                var permissionsToAdd = AppendPermissionsDistinct(permissions, IdentityScopes);
                var claims           = permissionsToAdd.Select(permission => CreateClaim("permission", permission)).ToList();
                claims.Add(CreateClaim("subject", user.UserName));

                foreach (var claim in claims)
                {
                    claim.UserId = user.Id;
                    await _securityRepository.AddClaim(claim);
                }
            }

            return(true);
        }