When you use the provided OAuth authentication for authenticating against a service provider, tokens will be cached so that when you make requests to service providers, you don't have to request another token and waste time. The provided mechanism for caching is an in-memory cache. This quick start will walk you through adding your own caching mechanism.
Adding caching
To add your own caching implementation, you need to create a class that implements the IScimCache
interface, as well as a class to represent a cache entry that implements the interface IScimCacheEntry
. Below is a crude example of an implementation
public class CustomCache : IScimCache
{
private List<CacheEntry> entries;
public CustomCache()
{
entries = new List<CacheEntry>();
}
public IScimCacheEntry CreateEntry(string key)
{
if (entries.Any(e => e.Key as string == key)) throw new Exception($"Key already exists: {key}");
var entry = new CacheEntry(key);
entries.Add(entry);
return entry;
}
public void Remove(string key)
{
var foundEntries = entries.Where(e => e.Key as string == key);
entries = entries.Except(foundEntries).ToList();
}
public bool TryGetValue(string key, out object value)
{
var foundEntry = entries.FirstOrDefault(e => e.Key as string == key);
value = foundEntry?.Value;
return foundEntry != null;
}
}
public class CacheEntry : IScimCacheEntry
{
public CacheEntry(string key)
{
Key = key;
}
public object Value { get; set; }
public object Key { get; }
}
To register this cache, call the AddCache
method on the ScimClientBuilder
public void ConfigureServices(IServiceCollection services)
{
...
ScimOAuthOptions oAuthOptions = new ScimOAuthOptions("scimclient", "https://localhost:5003/connect/token", "scimclient", "scim");
services.AddScimClient()
.AddUser<ClientUser, ClientUserMapper>()
.AddServiceProvider("ServiceProviderName", "https://localhost:5000/SCIM/", oAuthOptions)
.AddCache<CustomCache>();
...
}
Previous
Next