<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emil Pettersson</title>
    <description>The latest articles on DEV Community by Emil Pettersson (@emil_pettersson_e7280e3cd).</description>
    <link>https://dev.to/emil_pettersson_e7280e3cd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1553265%2F0fec284b-6d09-4244-a91b-89c76923b738.png</url>
      <title>DEV Community: Emil Pettersson</title>
      <link>https://dev.to/emil_pettersson_e7280e3cd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emil_pettersson_e7280e3cd"/>
    <language>en</language>
    <item>
      <title>Connecting Azure SQL-Database and App Service using system-assigned identity</title>
      <dc:creator>Emil Pettersson</dc:creator>
      <pubDate>Fri, 31 May 2024 08:44:01 +0000</pubDate>
      <link>https://dev.to/charliefoxtrot/connecting-azure-sql-database-and-app-service-using-system-assigned-identity-1jf</link>
      <guid>https://dev.to/charliefoxtrot/connecting-azure-sql-database-and-app-service-using-system-assigned-identity-1jf</guid>
      <description>&lt;p&gt;I had built an application using an Azure SQL-Database for storage and I wanted to upload it to Azure as an App Service to run it in the cloud. I had assumed it was going to be like connecting an App Service to BlobStorage or TableStorage in Azure, but it turns out it is a bit different for a SQL-Database. Primarily how the system-assigned identity is used to authenticate. After figuring out how to get it to work, I decided to write the short guide I myself would have wanted to find when I started googling on why I couldn’t connect to my database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the database
&lt;/h2&gt;

&lt;p&gt;To create a database we will first have to create a SQL-Server resource to host the database. When creating we will use the &lt;strong&gt;Authentication Method&lt;/strong&gt; “Use Microsoft Entra-only authentication”, and set ourselves as Admin for the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2ie9zdvvr8svvhut629.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2ie9zdvvr8svvhut629.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To allow other azure resources to access our new server we have to go to Security &amp;gt; Networking. There we select to give public network access to “Selected networks”.&lt;/p&gt;

&lt;p&gt;We then add our own IP Address to the Firewall Rules since we will want to test connecting to our SQL-Server locally. It is also needed to log in to our SQL-database in Azure Portal later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88rwzwd62f9xcre3a9rp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88rwzwd62f9xcre3a9rp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the special case that your App Service and SQL-Server will be in different Azure Subscriptions. you will have to check “Allow Azure services and resources to access this server” under Exceptions. Note that this is a big risk since it opens up your SQL-Server to EVERY Azure service and resource, even those belonging to other subscriptions and other Azure users, customers and organizations. To be secure there are solutions using virtual networks and configuring Private Access. But this is not included in the scope of this tutorial, so we will accept the shortcut of allowing this exception. This can also be a tool for troubleshooting if you suspect you have problems with connecting to the SQL-Server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0yf53g3doxgd21ffdvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx0yf53g3doxgd21ffdvf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next step is to create the SQL Database, so go to Overview and select Create Database. In the tutorial we assume the database is only for learning purposes so we want to keep costs as low as possible. So for Compute + Storage we select “Configure database” and slide the vCores and Data max Size to their lowest settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xonndbz7chsc48wrphp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xonndbz7chsc48wrphp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then for &lt;strong&gt;Backup storage redundancy&lt;/strong&gt; we select “Locally-redundant backup storage”, the weakest kind.&lt;/p&gt;

&lt;p&gt;When the resource has been created, go to &lt;strong&gt;Query editor (preview)&lt;/strong&gt; and log in with Microsoft Entra authentication. If it does not work make sure you have whitelisted your own IP.&lt;/p&gt;

&lt;p&gt;We then create a new table by running:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

CREATE TABLE animals (id INT PRIMARY KEY, name VARCHAR(100));


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This creates a very simple table called “animals” where we can list different kinds of animals. Add a few animals:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

INSERT INTO [dbo].[animals] (id, name) VALUES (1, 'Lion');
INSERT INTO [dbo].[animals] (id, name) VALUES (2, 'Fox');
INSERT INTO [dbo].[animals] (id, name) VALUES (3, 'Cat');


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can then see your animals by running:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

SELECT * FROM [dbo].[animals]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And you can count them by running:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

SELECT COUNT(*) FROM [dbo].[animals]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Creating the code
&lt;/h2&gt;

&lt;p&gt;We will now build a small C# program that connects to the SQL Database and runs the SQL-command for counting the animals. Create a new ASP.NET Core Empty project in Visual Studio.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqm6lfu0xyof5gvh1sh59.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqm6lfu0xyof5gvh1sh59.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This gives you a project with only a Program.cs with minimal amount of code. We will be able to write our whole program here. First thing to do is to install the NuGet Microsoft.Data.SqlClient;&lt;/p&gt;

&lt;p&gt;We then create a minimal api to connect to our SQL-database and execute the command to count our animals, and delete the auto-generated code we won’t need. This leaves us with a Program.cs like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnar53jc883w58g4th8sw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnar53jc883w58g4th8sw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we go to Overview in your database-resource and select “See connection strings” under &lt;strong&gt;Connect to application&lt;/strong&gt;. Take the connection string for “ADO.NET (Microsoft Entra passwordless authentication)” and put it into your appsettings of the newly created project.&lt;/p&gt;

&lt;p&gt;For me, where both SQL-Server and SQL-database are named “charlie-foxtrot-sql” it will look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0427soz71fqiweqz2bl0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0427soz71fqiweqz2bl0.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
(Note1: Treat your Connection Strings as secrets, the resources my connection string are referencing will have been deleted when this tutorial is published)&lt;br&gt;
(Note2: In the connection string you can see that we set authentication to be “Active Directory Default”. Entra ID used to be called Active Directory. This setting means that we can authenticate with our account assigned in visual studio when we run locally, and with managed identities given certain roles when we run it as an App Service)&lt;/p&gt;

&lt;p&gt;Then run your program and make a call to the /test-db endpoint. You can do this using swagger, your favorite testing tool (e.g Postman or Insomnia) or your web browser.&lt;/p&gt;

&lt;p&gt;If you get an error “Globalization Invariant Mode is not supported” you’ll have to unload your project and remove&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuam26w6oc1ndz3hcphhv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuam26w6oc1ndz3hcphhv.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
from your project file (or set it to false). Then load the project again.&lt;/p&gt;

&lt;p&gt;If you get trouble with authentication, make sure you are using the same account for Azure Service Authentication in Visual Studio as you used to create the SQL-Server and database in Azure, and that the account has the proper IAM-role to access them. You can check this under Access Control (IAM) for your SQL-Server.&lt;/p&gt;

&lt;p&gt;You should then get a response that corresponds to the value you get by running the count-command directly in the Query Editor in Azure Portal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi8mkl7ctw5jjrmoor2d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbi8mkl7ctw5jjrmoor2d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we will create an App Service to run this code in Azure.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating the App Service
&lt;/h2&gt;

&lt;p&gt;Create a repository for the code in Github (or Azure DevOps or Bitbucket), and push your code, except for your appsettings, to it. We will deploy the code to our App Service from there.&lt;/p&gt;

&lt;p&gt;In Azure Portal, go to App Services and press Create &amp;gt; Web App and create your App Service. Then we have to connect our App Service to Github so that it knows which code to deploy. We get two options for Github Action workflow to authenticate to our App Service when deploying; the default “User-assigned identity” and “Basic authentication”. If you have sufficient permissions to use “User-assigned identity” that is recommended. You need to be able to assign role-based access to the identity. If you lack these permissions you can instead use “Basic authentication”.&lt;/p&gt;

&lt;p&gt;If you use “Basic authentication” you first have to go to Settings &amp;gt; Configuration in your App Service and put SCM Basic Auth Publishing Credentials to &lt;strong&gt;On&lt;/strong&gt; and press Save. If you use “User-assigned identity” you don’t have to do this.&lt;/p&gt;

&lt;p&gt;Then go to Deployment &amp;gt; Deployment Center and choose your Source for the code. Choose your way of authentication and press Save.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimmv88jve7xoeq4tmxcz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fimmv88jve7xoeq4tmxcz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure that it builds. Then go to Settings &amp;gt; Environment variables and add your connection string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0je4r0x9ww9qahkgjoue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0je4r0x9ww9qahkgjoue.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure you click Apply once after adding this applications setting, then once again to confirm you are done editing your application settings in general.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note the naming of the Application Setting and compare it to how it is in your local appsettings.json. This is how Azure deals with hierarchy in JSON;&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"Level1": {
    "Level2": {
        "Level3:": "Value",
    }
},


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;would in the Azure App Service application settings become a setting with the name&lt;br&gt;
"Level1:Level2:Level3" and the value “Value”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Then go to Settings &amp;gt; Identity and set Status to &lt;strong&gt;On&lt;/strong&gt; for System Assigned identity. Remember to save.&lt;/p&gt;

&lt;p&gt;For most resources in Azure you would then give a role to this managed identity that let the App Service access it. This is called Role-Based Access Control (RBAC). But for Azure SQL Database we have to do it in a different way; We want to add our App Service as a user to the database and then give the user read and write permissions. So go to the Query Editor in your database and run&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

CREATE USER [&amp;lt;appservice&amp;gt;] FROM EXTERNAL PROVIDER;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;where  is the name of your App Service. Then run both&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ALTER ROLE db_datareader ADD MEMBER [&amp;lt;appservice&amp;gt;];
ALTER ROLE db_datawriter ADD MEMBER [&amp;lt;appservice&amp;gt;];


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;to give the new role proper permissions.&lt;br&gt;
(Note: In our code we are only reading from the database, but the common case is to want both read and write permissions.)&lt;/p&gt;

&lt;p&gt;Now you can go to your App Service Overview where you will see which “Default Domain” your app has&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfdtetcs5mp9v5digq02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfdtetcs5mp9v5digq02.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;go to the test-endpoint of this domain. My App Service is called charlie-foxtrot-sql so for me it is &lt;a href="https://charlie-foxtrot-sql.azurewebsites.net/test-db" rel="noopener noreferrer"&gt;https://charlie-foxtrot-sql.azurewebsites.net/test-db&lt;/a&gt;. This should now show you the number of animals in your database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2l7udl0cf45nx94pvzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2l7udl0cf45nx94pvzj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>azure</category>
      <category>sqlserver</category>
      <category>identity</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
