/// <summary>
        /// Foydalanuvchini to`kenini olish
        /// </summary>
        /// <param name="user">Foydalanuvchini ma`lumotlari</param>
        /// <returns></returns>
        private string GetToken(ApiUser user)
        {
            var token = new JwtSecurityToken(
                AuthConstants.ValidIssuer,
                AuthConstants.ValidAudience,
                new[] { new Claim(nameof(ApiUser.Id), user.Id.ToString()) },
                expires: DateTime.Now.AddMinutes(5),
                signingCredentials: new SigningCredentials(
                    AuthConstants.GetSecurityKey(),
                    SecurityAlgorithms.HmacSha256Signature
                    )
                );

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Apidagi servislarni sozlash
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            var parametres = new TokenValidationParameters()
            {
                ValidIssuer    = AuthConstants.ValidIssuer,
                ValidAudience  = AuthConstants.ValidAudience,
                ValidateIssuer = true,

                IssuerSigningKey = AuthConstants.GetSecurityKey(),
                ValidateLifetime = true
            };

            services.AddAuthentication(options => options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(o => o.TokenValidationParameters = parametres);
        }