Exemple #1
0
        //Token üretecek metot.
        public AccessTokenOutput CreateAccessToken(User user)
        {
            AccessTokenOutput tokenInstance = new AccessTokenOutput();

            //Security  Key'in simetriğini alıyoruz.
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Token:SecurityKey"]));

            //Şifrelenmiş kimliği oluşturuyoruz.
            SigningCredentials signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            //Oluşturulacak token ayarlarını veriyoruz.
            tokenInstance.Expiration = DateTime.Now.AddMinutes(5);
            JwtSecurityToken securityToken = new JwtSecurityToken(
                issuer: Configuration["Token:Issuer"],
                audience: Configuration["Token:Audience"],
                expires: tokenInstance.Expiration, //Token süresini 5 dk olarak belirliyorum
                notBefore: DateTime.Now,           //Token üretildikten ne kadar süre sonra devreye girsin ayarlıyouz.
                signingCredentials: signingCredentials
                );

            //Token oluşturucu sınıfında bir örnek alıyoruz.
            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

            //Token üretiyoruz.
            tokenInstance.AccessToken = tokenHandler.WriteToken(securityToken);

            //Refresh Token üretiyoruz.
            tokenInstance.RefreshToken = CreateRefreshToken();
            return(tokenInstance);
        }
Exemple #2
0
        /// <summary>
        /// Oauth Token fetch operation for driver
        /// </summary>
        /// <returns></returns>
        public async Task <string> FetchDriverToken(string mbeApiUrl)
        {
            HttpClient          client           = new HttpClient();
            EnvironmentSettings envConfiguration = GetEnvironmentSettings();

            string clientId     = envConfiguration.ClientDetails.DRIVERS.client_id;
            string clientSecret = envConfiguration.ClientDetails.DRIVERS.client_secret;

            var tokenValues = new Dictionary <string, string>
            {
                { "client_id", clientId },
                { "client_secret", clientSecret },
                { "grant_type", "client_credentials" },
                { "scope", "post" }
            };
            var tokenContent = new FormUrlEncodedContent(tokenValues);
            var response     = await client.PostAsync(mbeApiUrl + "oauth/token", tokenContent);

            var tokenResponse             = response.Content.ReadAsStringAsync();
            AccessTokenOutput tokenOutput = (AccessTokenOutput)JsonConvert.DeserializeObject(tokenResponse.Result, typeof(AccessTokenOutput));

            storedDriverToken = tokenOutput.access_token;

            return(tokenOutput.access_token);
        }