azure 尝试在ASP.NET中使用托管标识时,应用服务发送500.30错误

btqmn9zl  于 2022-12-04  发布在  .NET
关注(0)|答案(1)|浏览(173)

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.

bbuxkriu

bbuxkriu1#

检查以下步骤以使用ASP.Net中的托管标识访问Blob存储。

  • 已创建'ASP.NET CORE Web应用程序并将该应用程序部署到Azure应用程序服务。
  • 确保已在部署的应用程序服务上启用受管标识。

  • 我有一个Contibutor角色,赠款完全访问所有资源。所以,我没有设置任何权限。

  • 如果您使用MVC应用程序,请将代码写入Controller.cs,否则仅将其写入Program.cs文件。
  • 下面的代码试图获取容器中的blob
  • Storage Account中,创建一个容器。

  • 在你创建的Container中,上传blob。Blob只不过是你上传的file/doc


指令集

  • Storage Account =〉Access keys复制ConnectionString。单击显示,将显示复制connectionstring的图标。

我的程序. cs文件

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();
  • 重新部署应用程序并运行应用程序。
  • 在本地Console.WriteLine中,在输出窗口中显示输出。而在生产(Azure应用服务)中,它不显示任何输出。
    本地输出


指令集

相关问题