So I have used OneDrive with MS Graph and it has built in functions by default as shown below to store the current token in cache so you do not need to request it each time as shown below in program.cs:
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
builder.Configuration.Bind("AzureAd", options);
}).EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
THe AddInMemoryTokenCaches() function for the MS Graph Identity sdk stores the token in cache for later use and also the sdk handles getting a new refresh token if it expires by doing a redirect. However, I do not see such a function for Box API and I am curious the best approach to doing something like this. I am sure this could come up in other scenarios not just Box. My current approach is I initiate a service for a interface for box in my program.cs
builder.Services.AddTransient<IBoxDrive, BoxDrive>();
Then in this interface and extension of it I use dependency injection to get the config file and then request a new token every request. This seems so inefficient but I am not sure how to cache it and also if the token does expire how do I ensure I get a new refresh token without just requesting it every time like I do not. My interface is as follows:
namespace TestProject.Repositories
{ public interface IBoxDrive { Task FileInfoAsync(string BoxId); }
public class BoxDrive : IBoxDrive
{
private readonly IWebHostEnvironment _env;
private readonly BoxJWTAuth _session;
public BoxDrive(IWebHostEnvironment env) {
var _env = env;
var RootPath = _env.ContentRootPath;
var boxConfigLocation = Path.Combine(System.IO.Directory.GetParent(RootPath).ToString(), "box/699422622_746zo0rh_config.json");
var config = BoxConfigBuilder.CreateFromJsonString(System.IO.File.ReadAllText(boxConfigLocation)).Build();
_session = new BoxJWTAuth(config);
}
public async Task<BoxFile> FileInfoAsync(string BoxId)
{
BoxFile file = null;
try
{
var adminToken = await _session.AdminTokenAsync(); //valid for 60 minutes so should be cached and re-used
BoxClient adminClient = _session.AdminClient(adminToken);
file = await adminClient.FilesManager.GetInformationAsync(id: BoxId);
return file;
}
catch (Exception ex2)
{
return file;
}
}
}
}