Beispiel #1
0
        public string AccessTokenGenerator()
        {
            var AccessToken = string.Empty;
            var response    = new HttpResponseMessage();
            //Get URLs from App.Config
            string oAuthApiUrl = Utility.GetAppSettings(Constants.OAuthApiUrl);

            using (var oAuthClient = new HttpClient())
            {
                oAuthClient.BaseAddress = new Uri(oAuthApiUrl);
                //Generate JWS and get access token (JWT)
                OAuthGenerator.GenerateJWSAndGetAccessToken(oAuthClient);
                //Read OAuth API response
                response = oAuthClient.GetAsync(oAuthApiUrl).Result;
                if (response != null && response.IsSuccessStatusCode)
                {
                    var oauthApiResponse = response.Content.ReadAsAsync <AccessTokenResponse>().Result;
                    if (oauthApiResponse != null && oauthApiResponse.StatusCode == 200)
                    {
                        //Get Access token from OAuth API response
                        //Access token is valid for one hour
                        AccessToken = oauthApiResponse.AccessToken;
                        Session.SetString("AccessToken", AccessToken);
                    }
                }
                return(AccessToken);
            }
        }
Beispiel #2
0
        public static bool CheckIsJWTExpired(string JWT)
        {
            var tokenHandler       = new JwtSecurityTokenHandler();
            JwtSecurityToken token = new JwtSecurityToken();

            token = tokenHandler.ReadJwtToken(JWT);
            bool isJWTExpired = false;

            if (token != null && token.Claims != null && token.Claims.Any())
            {
                if (token.Claims.Any(x => x.Type == JwtRegisteredClaimNames.Exp))
                {
                    int expiryTime = Utility.GetInt(token.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.Exp).Value);

                    int currentTime = Utility.GetInt(OAuthGenerator.ConvertToUnixEpochString(DateTime.Now));

                    if (expiryTime < currentTime)
                    {
                        isJWTExpired = true;
                    }
                }
            }
            return(isJWTExpired);
        }