I have a SfSchedule that uses SfDatamanager with a url adapter to retrieve data from an API.
I recently cascaded authentication from the client wasm app to the back end api.
Here is the DataManager:
```
<SfDataManager Headers="@HeaderData" Url="@EventsUrl" CrudUrl="@EventsCrudUrl" UpdateUrl="@EventsUpdateUrl" RemoveUrl="@EventsRemoveUrl" InsertUrl="@EventsInsertUrl" Adaptor="Adaptors.UrlAdaptor"></SfDataManager>
```
Here is part of the oninitialized async:
```
var baseUrl = config.GetSection("APIUrl");
EventsUrl = baseUrl.Value + "scheduler";
EventsCrudUrl = baseUrl.Value + "batch";
EventsUpdateUrl = baseUrl.Value + "update";
EventsRemoveUrl = baseUrl.Value + "delete";
EventsInsertUrl = baseUrl.Value + "add";
var tokenResult = await tokenProvider.RequestAccessToken(new AccessTokenRequestOptions
{
Scopes = new[] { config["AzureAd:ApiScope"]! }
});
if (tokenResult.TryGetToken(out var accessToken))
{
HeaderData["Authorization"] = $"Bearer {accessToken.Value}";
}```
I am setting the Headers field of datamanager, however, when I look at the browser network tab I don't see the header being added to the request.
I verified that the token is being called and added with breakpoints in the application.
I am using the same Auth method in other services that call the backend api and have no issues with the token.
I was able to do a work around by specifying the HttpClientInstance for SFDatamanager as well as adding a custom message handler to add the auth header to every call that is made.
Still something wrong with Headers parameter not adding the header in sfdatamanager
<SfDataManager HttpClientInstance="@HttpClient" Url="@EventsUrl" CrudUrl="@EventsCrudUrl" UpdateUrl="@EventsUpdateUrl" RemoveUrl="@EventsRemoveUrl" InsertUrl="@EventsInsertUrl" Adaptor="Adaptors.UrlAdaptor"></SfDataManager>
public class AuthorizationMessageHandler(
IAccessTokenProvider tokenProvider,
NavigationManager navigationManager,
IEnumerable<string> scopes)
: DelegatingHandler
{
private readonly IAccessTokenProvider _tokenProvider = tokenProvider;
private readonly NavigationManager _navigationManager = navigationManager;
private readonly IEnumerable<string> _scopes = scopes;
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var tokenResult = await _tokenProvider.RequestAccessToken(new AccessTokenRequestOptions
{
Scopes = _scopes
});
if (tokenResult.TryGetToken(out var accessToken))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Value);
}
else
{
throw new InvalidOperationException("Authorization failed.");
}
return await base.SendAsync(request, cancellationToken);
}
}
builder.Services.AddScoped<AuthorizationMessageHandler>(sp =>
{
var tokenProvider = sp.GetRequiredService<IAccessTokenProvider>();
var navigationManager = sp.GetRequiredService<NavigationManager>();
return new AuthorizationMessageHandler(tokenProvider, navigationManager, new[] { builder.Configuration["AzureAd:ApiScope"]! });
});
builder.Services.AddHttpClient("ServerAPI", client =>
{
client.BaseAddress = new Uri(configuration["APIUrl"]!);
}).AddHttpMessageHandler<AuthorizationMessageHandler>();
Hi Daniel,
We suspect that you are expecting to include the headers to the url via SfDataManager. If so we suggest you to try using headers property of SfDataManager. Kindly check the below attached documentation and code for additional information.
Reference: https://blazor.syncfusion.com/documentation/data/how-to/adding-custom-headers
|
<SfDataManager Headers=@HeaderData Url="https://blazor.syncfusion.com/services/production/api/Orders/" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager> @code{
private IDictionary<string, string> HeaderData = new Dictionary<string, string>();
|
Kindly try using the above solution and let us know if you face any difficulties.
Regards,
Monisha