Пример #1
0
        public async Task <string> GetTokenAsync(string userName, string Password)
        {
            var tokenHandler      = new JwtSecurityTokenHandler();
            var key               = Encoding.ASCII.GetBytes(_configuration["JwtOptions:SecretKey"]);
            var specificationUser = new ApplicationUserSpecification(userName, Password);
            var user              = await _userContext.FindApplicationUser(specificationUser);

            var claims = new List <Claim>();

            if (user != null)
            {
                foreach (PropertyInfo prop in user.GetType().GetProperties())
                {
                    var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                    if (prop.Name != "DecryptedPassword" && prop.GetValue(user, null) != null)
                    {
                        claims.Add(new Claim(prop.Name, prop.GetValue(user, null).ToString() ?? ""));
                    }
                }

                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject            = new ClaimsIdentity(claims.ToArray()),
                    Expires            = DateTime.UtcNow.AddHours(8),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };
                var token = tokenHandler.CreateToken(tokenDescriptor);
                await Task.CompletedTask;
                return(tokenHandler.WriteToken(token));
            }

            await Task.CompletedTask;

            return(null);
        }
Пример #2
0
        public async Task <ApplicationUser> FindApplicationUser(Guid Id)
        {
            var specification = new ApplicationUserSpecification(Id);

            return(await this._asyncIdentityRepository.FindApplicationUser(specification));
        }
Пример #3
0
        public async Task <ApplicationUser> FindApplicationUser(Guid TypeIdentificationId, string Identification)
        {
            var specification = new ApplicationUserSpecification(TypeIdentificationId, Identification);

            return(await this._asyncIdentityRepository.FindApplicationUser(specification));
        }