public static ClaimsPrincipal Validate(string token) { _logger?.DebugMessage("Validating bearer token."); var jwt = new JwtSecurityToken(token); var userIdClaim = jwt.Claims.SingleOrDefault(c => c.Type == "userId"); if (userIdClaim != null) { _logger?.DebugMessage($"Validating user token: {userIdClaim?.Value}"); try { return(TokenValidatorV2.ValidateToken(token, _logger)); } catch (Exception ex) { _logger?.Exception(ex); throw; } } else { try { _logger?.DebugMessage($"Validating client token {jwt.Claims.SingleOrDefault(c => c.Type == "appid")?.Value}"); return(TokenValidatorV1.ValidateToken(token, _logger)); } catch (Exception ex) { _logger?.Exception(ex); throw; } } }
internal static void Exception(Exception exceptionToLog, string additionalInfo) { if (Logging.IsInstance()) { Logging.Exception(exceptionToLog, additionalInfo); } }
public static ClaimsPrincipal ValidateToken(string accessToken, ILogging logger) { var handler = new JwtSecurityTokenHandler(); //{ // Configuration = new SecurityTokenHandlerConfiguration { /*CertificateValidationMode = X509CertificateValidationMode.None*/ } //}; try { SecurityToken validatedToken; var securityToken = handler.ValidateToken(accessToken, new TokenValidationParameters { ValidAudience = Resource, ValidIssuers = new[] { B2CGlobalConfiguration.ValidIssuer, B2CGlobalConfiguration.ValidIssuer + "/" }, IssuerSigningKeys = Settings.SecurityTokens }, out validatedToken); var principal = new ClaimsPrincipal(securityToken); Thread.CurrentPrincipal = principal; logger?.DebugMessage($"User: {principal.Identity.Name}"); return(principal); } catch (Exception ex) { logger?.Exception(ex); throw new UnauthorizedAccessException("Unable to validate token", ex); } }
protected void Log(string query, Exception ex) { if (!OutputDebugLog) { return; } if (_logger != null) { _logger.DebugMessage($"Failed query: {query}", LogType.Information, this.GetType().FullName); _logger.Exception(ex, "CosmosDb gremlin connector"); } else { Logging.DebugMessage($"Failed query: {query}", LogType.Information, this.GetType().FullName); Logging.Exception(ex, this.GetType().FullName); } Console.WriteLine(query); }
protected bool Log(string query, Exception ex) { if (!OutputDebugLog) { return(false); } if (_logger != null) { _logger.DebugMessage($"Failed query: {query}", LogType.Information, this.GetType().FullName); _logger.Exception(ex, $"{GetType().FullName}({ex.GetType()})"); } else { Logging.DebugMessage($"Failed query: {query}", LogType.Information, this.GetType().FullName); Logging.Exception(ex, $"{GetType().FullName}({ex.GetType()})"); } Console.WriteLine(query); return(false); }
/// <summary> /// retrieves auto complete list /// ideal for listboxes or inteli. /// </summary> /// <param name="token">search text</param> /// <returns>List of Suggestions from Google</returns> public async Task <List <AutoCompleteResponse> > GetAutoCompleteAsync(string token) { _predictions = new List <AutoCompleteResponse>(); if (string.IsNullOrWhiteSpace(token)) { _logging.Error("GoogleApi:GetAutoCompleteAsync", "The search token can not be empty"); return(_predictions); } if (string.IsNullOrWhiteSpace(Key)) { _logging.Error("GoogleApi:GetAutoCompleteAsync", "The Google Key value is missing"); return(_predictions); } return(await Task.Run(async() => { try { string url = string.Format(AutoCompleteUrl, token, Key); var rawJson = await url.GetUrlResponseString(); var autoCompleteItems = rawJson.DeserializeObject <AutoComplete>(); if (autoCompleteItems != null && autoCompleteItems.predictions.Count > 0) { foreach (Prediction prediction in autoCompleteItems.predictions) { string placeOfInterest = GetLeftSplitValue(prediction.description); bool hasDescription = _predictions.Any(x => x.ShortName.Contains(placeOfInterest)); if (!hasDescription) { _predictions.Add(new AutoCompleteResponse { ShortName = placeOfInterest.Trim(), LongName = prediction.description, Id = prediction.place_id, PredictionTypes = prediction.types }); } } } return _predictions; } catch (Exception ex) { _logging.Exception("AutoCompleteRepository:Get", ex); _predictions = null; } return _predictions; })); }