我有一个托管在 Azure 上的 blazor 服务器应用程序,我正在使用 AAD 将我的用户登录到系统。
我还让用户创建文件夹并将文件上传到 Azure 文件共享。 这是我的问题。 当我从 Visual Studio 运行解决方案时,创建文件夹和上传文件没有问题。但是,如果我在从 VS 发布 web 应用程序后运行该应用程序,则会收到错误消息。
状态:403(该请求无权执行此操作。)
这是我正在使用的代码
// Initialize the ShareServiceClient with your connection string
ShareServiceClient serviceClient = new ShareServiceClient(_connectionString);
// Get a reference to the File Share
ShareClient shareClient = serviceClient.GetShareClient("projectfiles");
// Get a reference to the directory where you want to create the folder
ShareDirectoryClient directoryClient = shareClient.GetDirectoryClient(folderCreateString);
// Create the folder
directoryClient.CreateIfNotExists();
我的方法有这个标签 [Authorize(Policy = "Eventum.Users")]
我不是Azure专家,但猜测这是一个权限问题,我只是不知道如何修复它。
任何人都可以引导我走向正确的方向吗?
我使用 AAD 尝试了以下 Blazor 服务器代码,并能够在 Azure 存储文件共享中创建文件共享、文件夹和文件。
代码:
AzureStorageService.cs:
using Azure;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace XXXXXblazor
{
public class AzureStorageService
{
private readonly string connectionString;
public AzureStorageService(string connectionString)
{
this.connectionString = connectionString;
}
public async Task CreateShareAsync(string shareName)
{
var shareServiceClient = new ShareServiceClient(connectionString);
var shareClient = shareServiceClient.GetShareClient(shareName);
await shareClient.CreateAsync();
}
public async Task CreateDirectoryAsync(string shareName, string directoryName)
{
var shareServiceClient = new ShareServiceClient(connectionString);
var shareClient = shareServiceClient.GetShareClient(shareName);
var directoryClient = shareClient.GetDirectoryClient(directoryName);
await directoryClient.CreateAsync();
}
public async Task CreateFileAsync(string shareName, string directoryName, string fileName, Stream stream)
{
var shareServiceClient = new ShareServiceClient(connectionString);
var shareClient = shareServiceClient.GetShareClient(shareName);
var directoryClient = shareClient.GetDirectoryClient(directoryName);
var fileClient = directoryClient.GetFileClient(fileName);
await fileClient.CreateAsync(stream.Length);
await fileClient.UploadRangeAsync(new HttpRange(0, stream.Length), stream);
}
}
}
index.razor:
@page "/"
<h3>Azure Storage Demo</h3>
<button @onclick="CreateShare">Create Share</button>
<button @onclick="CreateDirectory">Create Directory</button>
<button @onclick="UploadFile">Upload File</button>
@code {
private string connectionString = "<Storage_Connec_string>";
private AzureStorageService storageService;
private string shareName = "<FileShare_name>";
private string directoryName = "<folder_name>";
private string fileName = "<file_name>.txt";
private async Task CreateShare()
{
storageService = new AzureStorageService(connectionString);
await storageService.CreateShareAsync(shareName);
}
private async Task CreateDirectory()
{
if (storageService == null)
{
storageService = new AzureStorageService(connectionString);
}
await storageService.CreateDirectoryAsync(shareName, directoryName);
}
private async Task UploadFile()
{
if (storageService == null)
{
storageService = new AzureStorageService(connectionString);
}
byte[] fileContent = GetFileContent("Hello, Azure Storage!");
using (var stream = new MemoryStream(fileContent))
{
await storageService.CreateFileAsync(shareName, directoryName, fileName, stream);
}
}
private byte[] GetFileContent(string content)
{
return System.Text.Encoding.UTF8.GetBytes(content);
}
}
输出:
运行成功如下:
我在浏览器中得到以下输出。然后,我单击创建共享、创建目录和上传文件在 Azure 存储中创建共享、目录和文件。
Azure 门户:
在Azure存储中创建的文件共享、目录和文件如下:
我将上述代码发布到Azure Web应用程序服务,如下所示:
我在下面的 Azure Active Directory 应用程序中添加了以下 重定向 URI:
然后,我浏览网络应用程序,如下所示:
我点击创建共享、创建目录和上传文件在Azure存储中创建共享、目录和文件,如下所示:
Azure 门户:
在 Azure 存储中创建的文件共享、目录和文件如下。