예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IdentityService"/> class.
 /// </summary>
 /// <param name="loggerFactory">A factory to create loggers from.</param>
 /// <param name="identifierValidationService">Provides validation logic for client-provided identifiers.</param>
 /// <param name="passwordValidationService">Provides validation logic for client-provided passwords.</param>
 /// <param name="passwordHashingService">The password hashing service.</param>
 /// <param name="identityRepository">A repository of identities.</param>
 public IdentityService(ILoggerFactory loggerFactory,
                        RoleService roleService,
                        IdentifierValidationService identifierValidationService,
                        PasswordValidationService passwordValidationService,
                        PasswordHashingService passwordHashingService,
                        IIdentityRepository identityRepository,
                        IIdentityRoleRepository identityRoleRepository)
 {
     Logger = loggerFactory.CreateLogger <IdentityService>();
     IdentifierValidationService = identifierValidationService;
     PasswordValidationService   = passwordValidationService;
     PasswordHashingService      = passwordHashingService;
     IdentityRepository          = identityRepository;
     IdentityRoleRepository      = identityRoleRepository;
     RoleService = roleService;
 }
예제 #2
0
        /// <summary>
        /// Creates a new identity.
        /// </summary>
        /// <param name="identifier">The unique user-chosen identifier with this identity.</param>
        /// <param name="password">The as-of-yet unhashed password of this identity.</param>
        /// <returns></returns>
        /// <exception cref="EntityAlreadyExsistsException">Identity</exception>
        public async Task <Identity> CreateIdentity(string identifier, string password)
        {
            // Validate identifier format and availability
            IdentifierValidationService.Validate(identifier);
            if ((await IdentityRepository.GetIdentity(identifier)) != null)
            {
                throw new EntityAlreadyExsistsException("Identity", identifier);
            }

            // Validate client-provided password
            PasswordValidationService.Validate(password);

            // Hash password and create new identity
            (string hash, byte[] salt) = PasswordHashingService.HashAndSaltPassword(password);
            return(await IdentityRepository.CreateIdentity(identifier, hash, salt));
        }