Search code examples
c#azureasp.net-coreazure-web-app-serviceazure-managed-identity

App Service sending 500.30 error when trying to use Managed Identity in ASP.Net


The customer is trying to use Managed Identity to access to Azure Resource by following the links below.

But they face the following error after updating the code:

"HTTP Error 500.30 - ASP.NET Core app failed to start" 

I also tried and faced the same issue. The customer is using 3.1 and I am using 6.0. (Without adding anything to use Managed Identity, there is no issue to deploy. Once adding something in code, it returns error message)

  1. Is there any step we should take which is not listed in the link above?

  2. Where to add the code in the link below? It is not "Program.cs"? Can anyone share the whole sample code? "https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview-for-developers?tabs=portal%2Cdotnet#accessing-a-blob-in-azure-storage"

  3. I am not familiar with .Net and I don't know where I can find "console.write" return in App Service. Could you please let me know? I don't get error in App Service with the below code, but once I add the code to upload file, I receive 500.30 error. I would like to check "blobClient1" is returning blob content or not, but I don't know where I can find the return(Try with Kudo, but not sure where to find).

var clientID = Environment.GetEnvironmentVariable("my client id");
var credentialOptions = new DefaultAzureCredentialOptions
{ ManagedIdentityClientId = clientID };

var credential = new DefaultAzureCredential(credentialOptions);

var blobServiceClient = new BlobServiceClient(new Uri(https://storageadfuat.blob.core.windows.net), credential);
BlobContainerClient containerClient1 = blobServiceClient.GetBlobContainerClient("blob container name");
BlobClient blobClient1 = containerClient1.GetBlobClient("blob name");

Console.Write("test", blobClient1);

Try to use Managed Identity with App Service and Azure Resources(ASP.NET). But once add the code by following the document, we receive 500.30 error message. Hopefully getting more clear sample code and manage to use the Managed Identity without any issue.


Solution

  • Check the below steps to use Managed Identity in ASP.Net to Access Blob Storage.

    • Created `ASP.Net CORE Web App and deploy the App to Azure App service.
    • Make sure Managed Identity is enabled on the deployed App Service.

    enter image description here

    • I have a Contibutor Role, which grants full access to all the resources. So, I haven't set any permissions.

    enter image description here

    • If you are using MVC Application, write the code in Controller.cs else write it in Program.cs file only.
    • The code which you are following is trying to get the blob which is in the container.
    • In Storage Account, create a container.

    enter image description here

    • Inside the Container which you have created, upload blobs. Blob is nothing but a file/doc which you have uploaded.

    enter image description here

    enter image description here

    • Copy the ConnectionString from Storage Account => Access keys .Click on show, icon to copy the connectionstring will be visible. enter image description here

    My Program.cs file

    using Azure.Identity;
    using Azure.Storage.Blobs;
    
    var builder = WebApplication.CreateBuilder(args);
    
    //Store the Connection string in `appsettings.json` and get the value here. For now I have given connection string in `Program.cs` itself.
     
    string mysaconn = "YourConnectionString from Storage Account";
    string containername = "mycontainer";
    
    BlobServiceClient serviceClient1 = new BlobServiceClient(mysaconn);
    BlobContainerClient containerClient1 = serviceClient1.GetBlobContainerClient(containername);
    var blobs = containerClient1.GetBlobs();
    foreach(var blob in blobs)
    {
        Console.WriteLine(blob.Name);
        BlobClient blobclient1 = containerClient1.GetBlobClient(blob.Name); 
    }
    Console.Read();
    
    builder.Services.AddRazorPages();
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");   
        app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapRazorPages();
    app.Run();
    
    • Re-deploy the App and run the Application.

    • In local Console.WriteLine shows the output in the Output window. And in production (Azure App Service) it doesn't show any output.

    Local Output

    enter image description here