bitwarden/server · error · BadRequestException

Unable to build callback Url

Error message

Unable to build callback Url

What it means

Thrown during Teams OAuth initiation when Url.RouteUrl returns null/empty for route name 'TeamsIntegration_Create'. Identical pattern to the Slack callback-URL error but for the Microsoft Teams integration. BadRequestException returns HTTP 400, preventing the OAuth redirect from being generated.

Source

Thrown at src/Api/Dirt/Controllers/TeamsIntegrationController.cs:43

    TimeProvider timeProvider) : Controller
{
    [HttpGet("{organizationId:guid}/integrations/teams/redirect")]
    public async Task<IActionResult> RedirectAsync(Guid organizationId)
    {
        if (!await currentContext.OrganizationOwner(organizationId))
        {
            throw new NotFoundException();
        }

        var callbackUrl = Url.RouteUrl(
            routeName: "TeamsIntegration_Create",
            values: null,
            protocol: currentContext.HttpContext.Request.Scheme,
            host: currentContext.HttpContext.Request.Host.ToUriComponent()
        );
        if (string.IsNullOrEmpty(callbackUrl))
        {
            throw new BadRequestException("Unable to build callback Url");
        }

        var integrations = await integrationRepository.GetManyByOrganizationAsync(organizationId);
        var integration = integrations.FirstOrDefault(i => i.Type == IntegrationType.Teams);

        if (integration is null)
        {
            // No teams integration exists, create Initiated version
            integration = await integrationRepository.CreateAsync(new OrganizationIntegration
            {
                OrganizationId = organizationId,
                Type = IntegrationType.Teams,
                Configuration = null,
            });
        }
        else if (integration.Configuration is not null)
        {
            // A Completed (fully configured) Teams integration already exists, throw to prevent overriding

View on GitHub (pinned to e93b962371)

Solutions

  1. Verify route 'TeamsIntegration_Create' is registered in the Teams controller.
  2. Configure UseForwardedHeaders so Request.Host/Scheme are correct behind a reverse proxy.
  3. Confirm routing middleware is properly ordered in the pipeline.
  4. Provide a configurable BaseUrl fallback instead of relying solely on Url.RouteUrl.

Example fix

// before
var callbackUrl = Url.RouteUrl("TeamsIntegration_Create", values: null, protocol: ..., host: ...);

// after — configuration-based fallback
var baseUri = _config["BaseUrl"];
var callbackUrl = !string.IsNullOrEmpty(baseUri)
    ? $"{baseUri}/integrations/teams/callback"
    : Url.RouteUrl("TeamsIntegration_Create", values: null, protocol: ..., host: ...);
Defensive patterns

Strategy: try-catch

Validate before calling

// Integration test verifying route resolution
[Fact]
public void TeamsCallbackRouteResolves()
{
    var url = _urlHelper.RouteUrl("TeamsIntegration_Create");
    Assert.False(string.IsNullOrEmpty(url));
}

Type guard

public static bool TeamsCallbackUrlResolvable(IUrlHelper helper) =>
    !string.IsNullOrEmpty(helper.RouteUrl("TeamsIntegration_Create"));

Try / catch

try
{
    await _teamsService.InitiateOAuthAsync(orgId);
}
catch (BadRequestException ex) when (ex.Message.Contains("callback Url"))
{
    _logger.LogCritical("Teams callback route misconfigured.");
    return Problem("Teams integration is not properly configured on the server.");
}

Prevention

When it happens

Trigger: Initiating the Teams OAuth flow where the route 'TeamsIntegration_Create' is not registered or cannot be resolved. Also triggered when Request.Scheme/Host are unavailable behind a proxy.

Common situations: New deployment without forwarded-headers middleware; route renamed without updating the routeName parameter; reverse proxy stripping Host headers; environment-specific base path mismatch.

Related errors


AI-assisted analysis of bitwarden/server@e93b962371 (2026-08-13). Data as JSON: /api/errors/abf1f39b839c9649. Report an issue: GitHub.