C# Azure Blob存储下载:服务器无法进行身份验证,但重试时确认

C# Azure Blob存储下载:服务器无法进行身份验证,但重试时确认,c#,azure,blobstorage,C#,Azure,Blobstorage,从本地PC使用客户端azure SDK时,我遇到了一个非常奇怪的问题 使用CloudBlobClient上载和删除文件的工作方式与charm类似,但当尝试下载文件时,我遇到了一个身份验证异常(403)。 但是,如果我重试相同的代码,下次它将正常工作 有人知道为什么吗 下面是一些显示问题的简化代码: string blobEndpoint = "http://???.blob.core.windows.net"; string accountName = "???"; st

从本地PC使用客户端azure SDK时,我遇到了一个非常奇怪的问题

使用CloudBlobClient上载和删除文件的工作方式与charm类似,但当尝试下载文件时,我遇到了一个身份验证异常(403)。 但是,如果我重试相同的代码,下次它将正常工作
有人知道为什么吗

下面是一些显示问题的简化代码:

    string blobEndpoint = "http://???.blob.core.windows.net";
    string accountName = "???";
    string accountKey = "???";

        while(retry)
        {
            FileStream stream = new FileStream("C:\\temp\\mydownload.jpg", FileMode.Append);

            try
            {
                CloudBlobClient blobClient =
                new CloudBlobClient(new Uri(blobEndpoint), new StorageCredentialsAccountAndKey(accountName, accountKey));

                CloudBlob blob = blobClient.GetBlobReference("documents/drive.png");

                blob.DownloadToStream(stream);

                retry = false;
            }
            catch (Exception ee)
            {
                if (MessageBox.Show(ee.Message.ToString(), "ARGH!", MessageBoxButtons.RetryCancel) != System.Windows.Forms.DialogResult.Retry)
                    retry = false;
            }
            finally
            {
                stream.Flush();
                stream.Close();
            }

        }
正在生成的异常是:

    Microsoft.WindowsAzure.StorageClient.StorageClientException was unhandled
  Message=Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
  Source=Microsoft.WindowsAzure.StorageClient
  StackTrace:
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.Execute()
       at Microsoft.WindowsAzure.StorageClient.RequestWithRetry.RequestWithRetrySyncImpl[TResult](ShouldRetry retryOracle, SynchronousTask`1 syncTask)
       at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteSyncTaskWithRetry[TResult](SynchronousTask`1 syncTask, RetryPolicy policy)
       at Microsoft.WindowsAzure.StorageClient.CloudBlob.DownloadToStream(Stream target, BlobRequestOptions options)
       at Microsoft.WindowsAzure.StorageClient.CloudBlob.DownloadToStream(Stream target)
       at TestAzure.Form1.button1_Click(Object sender, EventArgs e) in c:\users\richard.donaldson\documents\visual studio 2010\Projects\TestAzure\TestAzure\Form1.cs:line 41
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at TestAzure.Program.Main() in c:\users\richard.donaldson\documents\visual studio 2010\Projects\TestAzure\TestAzure\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.WebException
       Message=The remote server returned an error: (403) Forbidden.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at Microsoft.WindowsAzure.StorageClient.EventHelper.ProcessWebResponseSync(WebRequest req, EventHandler`1 handler, Object sender)
       InnerException: {"The remote server returned an error: (403) Forbidden."}

我在使用WindowsAzure SDK for PHP时也遇到过这个问题。你当时有没有注意到你的应用有什么奇怪的行为?即响应速度慢、主要网络延迟等

增加:
您是否也在快速创建多个连接,就像在一个循环中?

哈哈哈。嘎。我刚刚把它改成了HTTPS,现在它可以正常工作了。无论如何,我可能应该使用
SSL
,这样就不用担心了,但是对于一个格式良好的问题,对于代码示例,它应该使用
HTTP
端点non-less

+1。另外,使用“ARGH!”作为消息框标题也会得到+1个假想点数。不,只是一个连接。哈哈哈。嘎。我刚刚把它改成了HTTPS,现在它可以正常工作了。无论如何,我可能应该使用SSL,所以真的不用担心,但它应该与http端点一起工作