예제 #1
0
        public ClientTokenDto CreateTokenByClient(Client client)
        {
            var accessTokenExpiration = DateTime.Now.AddMinutes(_customTokenSetting.AccessTokenExpiration);             //Token Süresi

            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_customTokenSetting.SecurityKey));        //Belirlediğim string şifreyi simetrik şifreye çeviriyorum

            SigningCredentials signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); //İmzalıyorum.

            JwtSecurityToken jwtSecurityToken = new JwtSecurityToken
                                                (
                issuer: _customTokenSetting.Issuer, //Sağlayıcı
                expires: accessTokenExpiration,     //Token Süresi
                notBefore: DateTime.Now,            //Şuanki saatten itibaren tokena süresini ekle ve geçerli olsun
                claims: GetClaimsByClient(client),
                signingCredentials: signingCredentials
                                                );

            var handler = new JwtSecurityTokenHandler(); //Token Oluşturacak arkadaş.
            var token   = handler.WriteToken(jwtSecurityToken);

            var tokenDto = new ClientTokenDto
            {
                AccessToken           = token,
                AccessTokenExpiration = accessTokenExpiration
            };

            return(tokenDto);
        }
        public ClientTokenDto CreateToken(Client client)
        {
            var AccesTokenOmru = DateTime.Now.AddMinutes(_customTokenOptions.AccesTokenO);

            var SecuritKey = SignService.SimetrikAnahtar(_customTokenOptions.SecuritKey);
            SigningCredentials signingCredentials = new SigningCredentials(SecuritKey, SecurityAlgorithms.HmacSha256Signature);
            JwtSecurityToken   jwtSecurityToken   = new JwtSecurityToken(

                issuer: _customTokenOptions.Issuer,
                expires: AccesTokenOmru,
                notBefore: DateTime.Now,
                claims: GetClaimsByClient(client),
                signingCredentials: signingCredentials);

            var handler = new JwtSecurityTokenHandler();
            var token   = handler.WriteToken(jwtSecurityToken);

            var tokenDto = new ClientTokenDto
            {
                AccessToken = token,

                AccessTokenExpiration = AccesTokenOmru,
            };

            return(tokenDto);
        }