Skip to content

401 Invalid_token The issuer is invalid - Asp.Net Core 3.1, Angular, Identity, Azure App Service, Linux #23520

@richmhouse

Description

@richmhouse

Describe the bug

I have created a web app https://www.alpacabiriba.com using the Visual Studio 2019 Template (Create New Project | Asp.Net Core Web Application | Angular + Inidividual User Accounts). I published this to an Azure Linux App Service using the VS2019 wizzards.

It works fine in development. It also works fine in Azure except when I click on Fetch Data when I get WWW-Authenticate: Bearer error="invalid_token", error_description="The issuer 'https://www.alpacabiriba.com' is invalid"

As it's deployed to a Microsoft's own cloud environment shouldn't it work out of the box or failing this have clear instructions of what to change?

To Reproduce

Visual Studio 2019 Template (Create New Project | Asp.Net Core Web Application | Angular + Inidividual User Accounts)
Deploy to Azure App Service using VS2019 & Deployment Centre Wizzards.

I changed as little code as I could to try & get it working. The only changes were.

  1. I changed it to load the p12 SSL certificate from /var/ssl/private the Azure App Service put it in. This included my attempt to inline the AddApiAuthorization extension method to get round it insisting on accessing the certificate key from the appsettings.json rather than /var/ssl/private.

  2. I added some code to void Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) to apply database migrations when run in Azure.

var identityServicesServicesColection = services.AddIdentityServer();
identityServicesServicesColection
  .AddAspNetIdentity < ApplicationUser > ()
  .AddOperationalStore < ApplicationDbContext > ()
  //.ConfigureReplacedServices()
  .AddIdentityResources()
  .AddApiResources()
  .AddClients();
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (environment == Environments.Development)
{
  identityServicesServicesColection.AddSigningCredentials();
}
else
{
  var p12 = LoadP12Certificate();
  identityServicesServicesColection.AddSigningCredential(p12);
}
      private X509Certificate2 LoadP12Certificate()
        {
            const string azurePrivateCertificatesDirectory = "/var/ssl/private";
            if (! Directory.Exists(azurePrivateCertificatesDirectory))
            {
                throw new InvalidOperationException($"Directory {azurePrivateCertificatesDirectory} does not exist");

            }
             var privateCertificates = Directory.GetFiles(azurePrivateCertificatesDirectory);
            if (privateCertificates.Length < 1)
            {
                throw new IOException($"No private certificate files found in directory {azurePrivateCertificatesDirectory}");
            }
            var privateCertificateFile = privateCertificates.FirstOrDefault();
            // https://docs.microsoft.com/en-us/azure/app-service/configure-ssl-certificate-in-code
            byte[] bytes;
            try
            {
                bytes = File.ReadAllBytes(privateCertificateFile);
            }
            catch (Exception)
            {
                throw new IOException($"Cannot read certificate file '{privateCertificateFile}'");
            }

            try
            {
                var certificate = new X509Certificate2(bytes);
                return certificate;
            }
            catch (Exception)
            {
                throw new Exception($"Cannot create private certificate from file '{privateCertificateFile}'");
            }
        }
  public void ApplyAnyPendingMigrations(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
                context.Database.Migrate();

                if (context.Database.GetPendingMigrations().Any())
                {
                    context.Database.Migrate();
                }
            }

        }

Exceptions (if any)

WWW-Authenticate: Bearer error="invalid_token", error_description="The issuer 'https://www.alpacabiriba.com' is invalid"

Further technical details

  • ASP.NET Core version
    : 3.1
  • Include the output of dotnet --info
    .NET Core SDK (reflecting any global.json):
    Version: 3.1.301
    Commit: 7feb845744

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.301\

Host (useful for support):
Version: 3.1.5
Commit: 65cd789777

.NET Core SDKs installed:
2.0.0 [C:\Program Files\dotnet\sdk]
2.1.200 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.402 [C:\Program Files\dotnet\sdk]
2.1.503 [C:\Program Files\dotnet\sdk]
2.1.512 [C:\Program Files\dotnet\sdk]
2.1.513 [C:\Program Files\dotnet\sdk]
2.1.805 [C:\Program Files\dotnet\sdk]
3.1.301 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    Microsoft Visual Studio Community 2019
    Version 16.6.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions