当文件名包含特殊字符时,shareSasBuilder不起作用

问题描述

我需要允许用户下载存储在Azure文件共享上的特定文件。只要文件名不包含任何特殊字符,它就可以正常工作,但是如果文件名包含任何特殊字符(例如空格),则失败并显示AuthenticationFailed错误。

<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:19654741-801a-0014-1b04-9197c9000000 Time:2020-09-22T17:20:44.1980779Z
</Message>
<AuthenticationErrorDetail>
Signature did not match. String to sign used was r 2020-09-22T19:20:42Z /file/depotsracq/cegep/BA/ZOOM 2.pdf 2019-07-07
</AuthenticationErrorDetail>
</Error>

这是返回Uri的方法。我很确定这与特殊字符的编码有关。在ShareSasBuilder中,我传递的文件名不带编码(file.Path),但URL需要对特殊字符进行编码。我猜Azure期望特殊字符的编码方式不同。

    public Uri getFileUri(string filePath)
    {
        ShareDirectoryClient directory = shareClient.GetDirectoryClient(Path.GetDirectoryName(filePath));
        ShareFileClient file = directory.GetFileClient(Path.GetFileName(filePath));

        if (file.Exists())
        {
            var shareSasBuilder = new ShareSasBuilder
            {
                ShareName = directory.ShareName,FilePath = file.Path,ExpiresOn = DateTime.UtcNow.AddHours(+2)
            };

            shareSasBuilder.SetPermissions(ShareFileSasPermissions.Read);

            SasQueryParameters sasQueryParameters = shareSasBuilder.ToSasQueryParameters(sharedKeyCredential);

            UriBuilder fullUri = new UriBuilder(file.Uri)
            { 
                 Query = sasQueryParameters.ToString()
            };

            return fullUri.Uri;
        }
        else
        {
            return null;
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)