admin管理员组文章数量:1355099
I am using MSAL to acquire a token from AzureAD, and then I will use this token for every FlUrl call.
To keep the MSAL token auto-refresh working, I need to call MSAL's AcquireToken before every FlUrl call. So I am going to implement the BeforeCall event in the way bellow.
- Is this the right approach?
- Can I get CancellationToken in some way in
FlurlEventHandler.HandleAsync
?
public sealed class FlurlBeforeCallAuthProviderHandler : FlurlEventHandler
{
private static readonly NLog.Logger _log = NLog.LogManager.GetCurrentClassLogger();
private readonly IMsAuthService _msAuthService;
public FlurlBeforeCallAuthProviderHandler(IMsAuthService msAuthService)
{
_msAuthService = msAuthService;
}
public override async Task HandleAsync(FlurlEventType eventType, FlurlCall call)
{
_log.Debug("HandleAsync()");
AuthenticationResult authResult = await _msAuthService.AcquireToken(["api://XXXXXXX/API.Read"], 0,
???cancellationToken???);
call.Request.WithOAuthBearerToken(authResult.AccessToken);
}
}
and the cache registration:
container.RegisterFactory<IFlurlClientCache>(c => new FlurlClientCache()
// all clients:
.WithDefaults(builder =>
{
builder.EventHandlers.Add((FlurlEventType.BeforeCall, new FlurlBeforeCallAuthProviderHandler(c.Resolve<IMsAuthService>())));
.WithHeaders(new
{
User_Agent = "SciaDevOpsManager",
});
}), FactoryLifetime.Singleton);
本文标签: FlUrl AcquireToken before each requestMSAL authenticationStack Overflow
版权声明:本文标题:FlUrl AcquireToken before each request - MSAL authentication - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744031630a2579015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论