public async Task <Guid?> GetUserIdAsync(string identifier) { if (string.IsNullOrWhiteSpace(identifier)) { throw new ArgumentNullException(nameof(identifier)); } // Generate unique key for this identifier string key = $"{nameof(UserService)}_{nameof(GetUserIdAsync)}_{identifier}"; // See if the cache has this key value if (!cache.TryGetValue(key, out Guid? val)) { // Key doesn't exist, query for UserID val = await azureDirectoryService.GetUserIdAsync(identifier).ConfigureAwait(false); // Set value to cache so long as it's a valid Guid if (val.HasValue && val.Value != Guid.Empty) { cache.Set(key, val, TimeSpan.FromMinutes(5)); // Cached value only for certain amount of time } } return(val); }
public async Task <string> GetUserIdAsync(string identifier) { if (string.IsNullOrWhiteSpace(identifier)) { throw new ArgumentNullException(nameof(identifier)); } string key = $"{nameof(UserService)}_{nameof(GetUserIdAsync)}_{identifier}"; if (!cache.TryGetValue(key, out string val)) { var guid = await azureDirectoryService .GetUserIdAsync(identifier) .ConfigureAwait(false); val = guid?.ToString(); if (!string.IsNullOrEmpty(val)) { cache.Set(key, val, TimeSpan.FromMinutes(5)); } } return(val); }
public async Task <string> GetUserIdAsync(string identifier) { if (string.IsNullOrWhiteSpace(identifier)) { throw new ArgumentNullException(nameof(identifier)); } // handle passing in the id as a string if (Guid.TryParse(identifier, out var userId)) { return(userId.ToString()); } string key = $"{nameof(UserService)}_{nameof(GetUserIdAsync)}_{identifier}"; if (!cache.TryGetValue(key, out string val)) { var guid = await azureDirectoryService.GetUserIdAsync(identifier).ConfigureAwait(false); val = guid?.ToString(); if (!string.IsNullOrEmpty(val)) { cache.Set(key, val, TimeSpan.FromMinutes(5)); // Cached value only for certain amount of time } } return(val); }
private async Task <Guid?> GetUserIdAsync(string identifier) { if (string.IsNullOrWhiteSpace(identifier)) { throw new ArgumentNullException(nameof(identifier)); } Guid?val = null; // Generate unique key for this identifier string key = $"{nameof(UserService)}_{nameof(GetUserIdAsync)}_{identifier}"; // See if the cache has this key value if (!cache.TryGetValue <Guid?>(key, out val)) { // Key doesn't exist, query for UserID val = await azureDirectoryService.GetUserIdAsync(identifier); // Set value to cache so long as it's a valid Guid if (val.HasValue && val.Value != Guid.Empty) { cache.Set(key, val); } } return(val); }
protected async Task <User> GetUserAsync(string identifier = null) { Guid?userId = default; if (string.IsNullOrEmpty(identifier)) { var token = await AcquireTokenAsync() .ConfigureAwait(false); var jwtToken = new JwtSecurityTokenHandler() .ReadJwtToken(token); if (jwtToken.Payload.TryGetValue("oid", out var oidValue) && Guid.TryParse(oidValue.ToString(), out Guid oid)) { userId = oid; } } else { userId = await AzureDirectoryService .GetUserIdAsync(identifier) .ConfigureAwait(false); } return(userId.HasValue ? new User() { Id = userId.Value.ToString() } : null); }