private string GetRawToken() { string rawToken = OAuthClientModule.GetAccessToken(this._realm, false); var tokenHandler = new SimpleWebTokenHandler(); var token = tokenHandler.ReadToken(Encoding.ASCII.GetString(Convert.FromBase64String(rawToken))); if (DateTime.Compare(token.ValidTo, DateTime.UtcNow.Add(_skew)) <= 0) { rawToken = OAuthClientModule.GetAccessToken(this._realm, true); } return rawToken; }
/// <summary> /// This method parses the incoming token and validates it. /// </summary> /// <param name="accessToken">The incoming access token.</param> /// <param name="error">This out paramter is set if any error occurs.</param> /// <returns>True on success, False on error.</returns> protected bool ReadAndValidateToken(string accessToken, string key, out ResourceAccessErrorResponse error) { bool tokenValid = false; error = null; SecurityToken token = null; ClaimsIdentityCollection claimsIdentityCollection = null; try { SimpleWebTokenHandler handler = new SimpleWebTokenHandler(); // read the token token = handler.ReadToken(accessToken); // validate the token claimsIdentityCollection = handler.ValidateToken(token, key); // create a claims Principal from the token IClaimsPrincipal authenticatedClaimsPrincipal = ServiceConfiguration.ClaimsAuthenticationManager.Authenticate( HttpContext.Current.Request.Url.AbsoluteUri, new ClaimsPrincipal(claimsIdentityCollection)); if (authenticatedClaimsPrincipal != null) { tokenValid = true; // Set the ClaimsPrincipal so that it is accessible to the application SetPrincipal(authenticatedClaimsPrincipal); } } catch (InvalidTokenReceivedException ex) { error = new ResourceAccessErrorResponse(OAuthConfiguration.Configuration.ServiceSettings.Realm.ToString(), ex.ErrorCode, ex.ErrorDescription); } catch (ExpiredTokenReceivedException ex) { error = new ResourceAccessErrorResponse(OAuthConfiguration.Configuration.ServiceSettings.Realm.ToString(), ex.ErrorCode, ex.ErrorDescription); } catch (Exception ex) { error = new ResourceAccessErrorResponse(OAuthConfiguration.Configuration.ServiceSettings.Realm.ToString(), OAuthConstants.ErrorCode.InvalidToken, "Token validation failed: " + ex.Message); } return(tokenValid); }
/// <summary> /// This method parses the incoming token and validates it. /// </summary> /// <param name="accessToken">The incoming access token.</param> /// <param name="error">This out paramter is set if any error occurs.</param> /// <returns>True on success, False on error.</returns> protected bool ReadAndValidateToken( string accessToken, string key, out ResourceAccessErrorResponse error ) { bool tokenValid = false; error = null; SecurityToken token = null; ClaimsIdentityCollection claimsIdentityCollection = null; try { SimpleWebTokenHandler handler = new SimpleWebTokenHandler(); // read the token token = handler.ReadToken( accessToken ); // validate the token claimsIdentityCollection = handler.ValidateToken( token, key ); // create a claims Principal from the token IClaimsPrincipal authenticatedClaimsPrincipal = ServiceConfiguration.ClaimsAuthenticationManager.Authenticate( HttpContext.Current.Request.Url.AbsoluteUri, new ClaimsPrincipal( claimsIdentityCollection ) ); if ( authenticatedClaimsPrincipal != null ) { tokenValid = true; // Set the ClaimsPrincipal so that it is accessible to the application SetPrincipal( authenticatedClaimsPrincipal ); } } catch ( InvalidTokenReceivedException ex ) { error = new ResourceAccessErrorResponse(OAuthConfiguration.Configuration.ServiceSettings.Realm.ToString(), ex.ErrorCode, ex.ErrorDescription ); } catch ( ExpiredTokenReceivedException ex ) { error = new ResourceAccessErrorResponse(OAuthConfiguration.Configuration.ServiceSettings.Realm.ToString(), ex.ErrorCode, ex.ErrorDescription); } catch ( Exception ex) { error = new ResourceAccessErrorResponse(OAuthConfiguration.Configuration.ServiceSettings.Realm.ToString(), OAuthConstants.ErrorCode.InvalidToken, "Token validation failed: " + ex.Message); } return tokenValid; }