This post is going to cover taking the existing set of applications we have been using to learn about Identity Server and deploying them to Azure. The starting point of the code can be found here.
Prep Work
The applications as they stand from the link above are not ready to be pushed to Azure most due to some configuration changes that are needed. We will go through each of the applications and take the hard-coded values and move them to appsettings.json.
API Application Configuration
The API application needs two configuration values for the address of the Identity Application and the address of the Client Application. The following two lines need to be added to the application’s appsettings.json file.
"IdentityServerAddress": "http://localhost:5000", "ClientAddress": "http://localhost:5002"
Then in the Startup class, the values need to be used. The Identity Server address is used in the JWT Bearer setup.
Before: o.Authority = "http://localhost:5000"; After: o.Authority = Configuration["IdentityServerAddress"];
Then the Client address is used in the CORS setup.
Before: policy.WithOrigins("http://localhost:5002") After: policy.WithOrigins(Configuration["ClientAddress"])
Identity Application Configuration
The Identity application needs a configuration value for the address of the address of the Client Application. The following line needs to be added to the application’s appsettings.json file.
"ClientAddress": "http://localhost:5002"
Next, the Config class needs a reference to configuration passed into the GetClients function.
public static IEnumerable<Client> GetClients(IConfiguration configuration)
Next, the references to http://localhost:5002 need to be replaced with the value from the configuration. The following is one example.
Before: RedirectUris = { "http://localhost:5002/signin-oidc" }, After: RedirectUris = { $"{configuration["ClientAddress"]}/signin-oidc" },
Identity Application Entity Framework
As part of publishing this set of applications, this example is going to use Azure SQL and right now the application is set up to use SQLite. In the Startup class replace UseSqlite with UseSqlServer. The following is an example of one of the needed replacements.
Before: options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); After: options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
When switching database providers make sure to delete and recreate your database migrations. I didn’t to begin with and it cost me a lot of time in changing down a strange error which this post covers.
Client Application Configuration
The Client application needs two configuration values for the address of the Identity Application and the address of the API Application. The following two lines need to be added to the application’s appsettings.json file.
"IdentityServerAddress": "http://localhost:5000", "ApiAddress": "http://localhost:5001/"
Then in the Startup class, the Identity Server Address needs to be used in the AddOpenIdConnect call.
Before: options.Authority = "http://localhost:5000"; After: options.Authority = Configuration["IdentityServerAddress"];
Next, the configuration values need to be passed to the Angular application. This process ended up being harder to figure out that I had anticipated and turned into a full blog post on its own. See this post for the details. The code for all the changes will also be on GitHub in case you need to the the diff for the client application.
Publish to Azure
Right-click on the Identity Application and select Publish.
This will show the Publish screen which provides the option to publish to Azure. We don’t have an existing App Service so we are going to create a new one. This page in the official docs explains all the options available on the publish screen. Click the publish button to continue.
The next screen that shows is the Create App Service Screen. I used all the default values and created a new Resource Group and App Service Plan. Keep in mind that the resource group and plan will be reused for the remaining two applications we are looking deploy. The only thing that will change between the applications on this screen will be the App Name.
The services tab looks like the following.
Next in the additional resources box lets hit the plus button next to SQL Database since our group of applications is going to need somewhere to store data. This will take us to the Configure SQL Database screen.
Since I don’t already have a SQL Server setup I am going to hit the New button to add one. That results in the next screen where you enter a name for the server as well as a username and password. After entering the required information click OK.
This will put you back on the configure database screen with most of it filled out. Make sure to set the database name you want to use.
Finally back on the Create App Service screen, you will see all the resources that you selected and configured. When you are satisfied with what you see click the Create button and let Azure do its magic.
When it is done you will see the profile now listed on the Publish page.
The above needs to be repeated for both the API and Client Applications, but using the Resource Group and App Service plan created above. Each profile should use a unique application name.
Identity Application Azure Configuration
The Identity Application needs access to the database that we created above. This means we need to set the DefaultConnection. The first step is to determine what the connection string should be. On the Azure Portal in your list of resources select the SQL database that we created above.
On the next page copy the provided connection string. Now navigate to the Identity App Service and under the Settings section select Application settings. Scroll down and find the Connection strings section and enter the copied value as the DefaultConnection.
Just above the Connection strings section we also need to enter a few values in the App settings section. For the Identity Application, we need the Twitter key and secret as well as the address of the client application. The following is a screenshot minus the actual values.
For the ClientAddress use the URL found in the Overview of the Client App’s App Service page.
API Application Azure Configuration
From the list of resources select the API App’s App Service page and in the Settings section select Application settings. In the App settings section add values for IdentityServerAddress and ClientAddress. As with the ClientAddress above the URLs for each application can be found on their respective App Service pages.
Client Application Azure Configuration
From the list of resources select the Client App’s App Service page and in the Settings section select Application settings. In the App settings section add values for IdentityServerAddress and ApiAddress.
Wrapping Up
At this point, you should be able to load up the application at the client address provided by Azure and have a working application. Overall the deployment to Azure was pretty easy. Getting the applications prepared to be deployed was a bit more challenging and sent me down a couple of rabbit holes. The code in its final state can be found here.
Also published on Medium.
Hi,
At which point you update the localhot urls to the actual xxx.azurewebsite.net urls?
Thanks
If you look at the Identity Application Azure Configuration section to the end of the post that is where it is talking about setting the URLs on the Azure side.
I’m new to IdentityServer, I have an Azure website using HTTPS already, do I still need to anything special installing certificate key used for signing tokens when I deploy IdentityServer to Azure? Thank you!
Hey Ray! I’m not sure. I hoping someone else might jump in with an answer.