Пример #1
0
        ///<inheritdoc />
        public object GetToken(string login, string password)
        {
            var identity = GetIdentity(login, password);

            if (identity == null)
            {
                return new { errorText = "Invalid username or password." }
            }
            ;

            var now = DateTime.UtcNow;
            // создаем JWT-токен
            var jwt = new JwtSecurityToken(
                AuthOptions.ISSUER,
                AuthOptions.AUDIENCE,
                notBefore: now,
                claims: identity.Claims,
                expires: now.Add(TimeSpan.FromMinutes(AuthOptions.LIFETIME)),
                signingCredentials: new SigningCredentials(GenerateSecurityKey.GetSecurityKey(), SecurityAlgorithms.HmacSha256));
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            var response = new
            {
                access_token = encodedJwt,
                username     = identity.Name
            };

            return(response);
        }
        /// <summary>
        ///     Получить параметры токена
        /// </summary>
        /// <returns> TokenValidationParameters. </returns>
        public static TokenValidationParameters GetTokenParameters()
        {
            var issuer = AuthOptions.ISSUER;

            return(new TokenParametersBuilder()
                   .IsValidateIssuer(issuer)
                   .IsValidateAudience()
                   .IsValidateLifetime()
                   .IsValidateIssuerSigningKey(GenerateSecurityKey.GetSecurityKey())
                   .Value);
        }