Umbraco-CMS: Umbraco 14 - "The specified 'redirect_uri' is not valid for this client application." error when trying to login after a certain time period

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

14.0.0

Bug summary

Getting an error:invalid_request after leaving an Umbraco site running for a while when trying to login. Error appears for any login method (local and SSO).

Specifics

Might be related to: https://github.com/umbraco/Umbraco-CMS/issues/16179; but I am not visiting the site behind a proxy.

Error image: image

Steps to reproduce

It appears to happen randomly after leaving the website running after a while.

Try to login after an unspecified amount of time.

Expected result / actual result

Expecting to be able to login, actual is error returned in json format.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 31 (1 by maintainers)

Most upvoted comments

Try set the “BackOfficeHost” and “UmbracoApplicationUrl” - We do not experience it after those two where set 😃

  "Umbraco": {
    "CMS": {
      "WebRouting": {
        "UmbracoApplicationUrl": "SET YOUR URL"
      },
      "Security": {
        "BackOfficeHost": "SET YOUR URL"
      }
    }
  },

@dutchbreeze

So basically, if you put this in your startup.cs, you’re able to use a custom OpenIddictApplicationManager

if (!environment.IsProduction())
{
  services.AddOpenIddict()
    .AddCore(options =>
    {
      options.ReplaceApplicationManager<CustomOpenIddictApplicationManager>();
    });
}

Just make sure to place it before services.AddUmbraco()

I also see in the PR that I added these two lines in the startup’s configure:

application.UseAuthorization();
application.UseCertificateForwarding();

I don’t remember if they truly had an impact.

And then the custom manager is pretty simple, check if the link is localhost or your dev environment and return true.

public class CustomOpenIddictApplicationManager : OpenIddictApplicationManager<OpenIddictEntityFrameworkCoreApplication>
{
  private readonly IWebHostEnvironment _environment;
  private readonly ILogger<OpenIddictApplicationManager<OpenIddictEntityFrameworkCoreApplication>> _logger;

  public CustomOpenIddictApplicationManager(
      IOpenIddictApplicationCache<OpenIddictEntityFrameworkCoreApplication> cache,
      ILogger<OpenIddictApplicationManager<OpenIddictEntityFrameworkCoreApplication>> logger,
      IOptionsMonitor<OpenIddictCoreOptions> options,
      IOpenIddictApplicationStoreResolver resolver,
      IWebHostEnvironment environment)
      : base(cache, logger, options, resolver)
  {
    _environment = environment;
    _logger = logger;
  }

  public override ValueTask<bool> ValidateRedirectUriAsync(OpenIddictEntityFrameworkCoreApplication application, string address, CancellationToken cancellationToken)
  {
    if (!_environment.IsProduction())
    {
      var uri = new Uri(address);
      if (uri.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase) ||
        uri.Host.Equals("host.docker.internal", StringComparison.OrdinalIgnoreCase))
      {
        return ValueTask.FromResult(true);
      }

      if (address.StartsWith(<your-dev-environment-url>))
      {
        return ValueTask.FromResult(true);
      }
    }

    return base.ValidateRedirectUriAsync(application, address, cancellationToken);
  }

  public override ValueTask<bool> ValidatePostLogoutRedirectUriAsync(OpenIddictEntityFrameworkCoreApplication application, string address, CancellationToken cancellationToken)
  {
    if (!_environment.IsProduction())
    {
      var uri = new Uri(address);
      if (uri.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase) ||
          uri.Host.Equals("host.docker.internal", StringComparison.OrdinalIgnoreCase))
      {
        return ValueTask.FromResult(true);
      }

      if (address.StartsWith(<your-dev-environment-url>))
      {
        return ValueTask.FromResult(true);
      }
    }

    return base.ValidatePostLogoutRedirectUriAsync(application, address, cancellationToken);
  }
}

I also noticed that I let this part in the appsettings while looking for a solution for our local and dev appsettings I don’t know if they’re absolutely necessary, but I didn’t feel like wasting more time testing it out.

"Umbraco": {
  "CMS": {
   _...other things_
    "Security": {
      "BackOfficeHost": "[url to your local or dev environment ]",
      "AllowConcurrentLogins": true
    },
    "WebRouting": {
      "UmbracoApplicationUrl": "[url to your local or dev environment]"
    }
  },
_...rest of the things_

Obviously this will be taken out when we deploy in production. I strongly suggest everyone does the same. Also a good idea to have additional security measures in place.

After some more testing seems like the trigger wasn’t enough and I needed to add a job that runs every x time and run stored procudure with the follwing query for my dev env : (for every DB i put all domain accessing that DB, you can add as many urls to the array’s)

UPDATE [dbo].[umbracoOpenIddictApplications]
  SET RedirectUris = '["https://myDomain/umbraco/oauth_complete","https://localhost:44383/umbraco/oauth_complete"]',
      PostLogoutRedirectUris = '["https://myDomain/umbraco/oauth_complete","https://myDomain/umbraco/logout","https://localhost:44383/umbraco/oauth_complete","https://localhost:44383/umbraco/logout"]'
  WHERE ClientId = 'umbraco-back-office'

This issue has been mentioned on Umbraco community forum. There might be relevant details there:

https://forum.umbraco.com/t/publishing-site-on-aws-fargate/3668/4

@akarkaselis Just implemented this one, and at first glance it seems to work! Great! Thanks again for sharing!

Just to add up, I dont think these do anything: application.UseAuthorization(); application.UseCertificateForwarding();

I’ll let you know if I find anything while testing!

@akarkaselis I did not have the change to test, but will in the next couple of days. I’ll let you know the outcome. Thanks a lot for sharing!

I just decided to override the validation for local and dev environments until the site is in production. It’s been a real headache having to restart the site every time we wanted to access the backoffice from localhost or dev. With 2-3 devs working on the site, it was downright impossible.

If you inherit from "OpenIddictApplicationManager < OpenIddictEntityFrameworkCoreApplication > " you can override the “ValidateRedirectUriAsync” (and “ValidatePostLogoutRedirectUriAsync”), check the environment and simply return true.

Obviously, this needs to be taken out when the site will go into production.

@Strike3Digital It seems that a reboot does affect the database. The redirect url seems to be reset after a reboot. However, this is our opinion at first glance and is not thoroughly tested.

Yes, from the looks of it the error message occurs right when the value gets changed in the umbracoOpenIddictApplications table.

Thank you for looking into it @dutchbreeze!

Hi @jovanzlatanovic , thanks a lot for the substantiation. Also good pointing out that table, I was still in the assumption that the openid connect options are used for the redirect url (callbackpath).

  • Indeed, it seems that when local dev is started, the redirect uris are reset to the localhost, resulting in the error happening on the staging env.
  • When restarting staging env, the redirect uris are set back to this url and we experience no errors.

At first glance it seems that the plain Umbraco login is also using the value from the umbracoOpenIddictApplications table. I conclude this from the fact that the error messages are occurring at the same pace. Can you acknowledge that?

Keep in mind that these conclusions are the result of a very short survey after your post.

Thanks again!

Hi @dutchbreeze, it would be in the umbracoOpenIddictApplications table.

Running an environment with a certain database sets the correct RedirectUris value.

After I try to run a different environment (on a different URL) the row with the umbraco-back-office value in the table gets the RedirectUris overwritten with the different environment.

I believe the reason sites work again after restarting is due to the fact when we run an environment again the value will get overwritten again.

And the “random amount of time” which is happening at which the sites break again is due to the fact it depends on when the other site using the same database gets run again.

Hope this helps with the investigation!

Hi @jovanzlatanovic, thank you for your message. We are not using appsettings.Release.json. We use config transformation during deployment.

Our situation is that the site works fine for a while and all of a sudden the issue pops up. Only way to solve it is to restart.

Hi all, we are still seeing this error even though we’ve set both BackOfficeHost and UmbracoApplicationUrl. We are running the latest 14.3.0