We currently have an API that is authenticated using Azure Active Directory. This API is accessed by our Teams App and call the Microsoft Graph (utilizing the On-Behalf-Of flow). We are using Asp.Net Core SignalR (dotnet 6) at the moment, but we would like to transition to Azure SignalR.
We have set up the API according to this documentation, and it is functioning as expected.
My program.cs look like this when configuring the authentication
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
builder.Services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
// We don't validate issuer as the application is multi tenant and can be any Azure AD tenant
// We don't validate audience as the application should use the OnBehalf flow
ValidateIssuer = false,
ValidateAudience = false,
};
var originalOnMessageReceived = options.Events.OnMessageReceived;
options.Events.OnMessageReceived = async context =>
{
await originalOnMessageReceived(context);
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments(Hub.Url))
{
context.Token = accessToken;
}
};
});
My Hub is configured as below
[Authorize]
public class TeamsHub : Hub
{
private readonly GraphServiceClient _client;
public TeamsHub(GraphServiceClient client)
{
_client = client;
}
public async Task ConnectUser(Guid meetingId)
{
if (Context.User == null) return;
var userAadId = Context.User.Claims.GetUserAadId();
await _client.Users[userAadId.ToString()].Presence.SetPresence("Available", "Available").Request().PostAsync();
}
}
My signalR is configured as below
builder.Services.AddSignalR(o =>
{
// register the middleware as filter to enable the multi-tenant with EF Core
o.AddFilter<MultiTenantServiceMiddleware>();
o.AddFilter<EventLoggingHubFilter>();
o.EnableDetailedErrors = true;
}).AddJsonProtocol(options =>
{
options.PayloadSerializerOptions.Converters
.Add(new JsonStringEnumConverter());
});
Everything works well with Asp.Net Core SignalR. However, as soon as I add AddAzureSignalR()
, the connection works, I receive the event, but the request to the Graph fails, stating that my user needs to perform an incremental consent.
Is there anything that i am missing ?