简体   繁体   English

尝试上传文件时出现“远程服务器返回错误:(401)未经授权”错误

[英]“The remote server returned an error: (401) Unauthorized” error when trying to upload a file

I found a lot of question about (401) Unauthorized error, but none of them explained to me how to diagnose the issue. 我发现了很多有关(401)未经授权的错误的问题,但是没有一个问题向我解释了如何诊断问题。

I created a new MVC ASP.net app to learn how to upload a file to a sharepoint folder. 我创建了一个新的MVC ASP.net应用程序,以了解如何将文件上传到sharepoint文件夹。 As of right now, I can't seem to be able to copy a file from C:\\testfolder\\file.txt to C:\\testfolder\\UploadedFiles 截至目前,我似乎无法将文件从C:\\ testfolder \\ file.txt复制到C:\\ testfolder \\ UploadedFiles

Here is the method that I made where I send in the source file path and the target folder: 这是我在源文件路径和目标文件夹中发送位置的方法:

 //Flag to indicate whether file was uploaded successfuly or not
        bool isUploaded = true;
        try
        {
            // Create a PUT Web request to upload the file.
            WebRequest request = WebRequest.Create(targetDocumentLibraryPath);

            //Set credentials of the current security context
            request.PreAuthenticate = true;
            request.UseDefaultCredentials = true;
            ICredentials credentials = new NetworkCredential( "Username", "password", "Domain"); //I used my username and password here
            request.Credentials = credentials;
            //request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "PUT";

            // Create buffer to transfer file
            byte[] fileBuffer = new byte[1024];

            // Write the contents of the local file to the request stream.
            using (Stream stream = request.GetRequestStream())
            {
                //Load the content from local file to stream
                using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))
                {
                    //Get the start point
                    int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
                    for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
                    {
                        stream.Write(fileBuffer, 0, i);
                    }

                }
            }

            // Perform the PUT request
            WebResponse response = request.GetResponse();

            //Close response
            response.Close();
        }
        catch (Exception ex)
        {
            //Set the flag to indiacte failure in uploading
            isUploaded = false; //The remote server returned an error: (401) Unauthorized.
        }

        //Return the final upload status
        return isUploaded; 
    }

I tried the following: 我尝试了以下方法:

  • Changing the permission of the folders to be able to write/read from them for all users. 更改文件夹的权限,以便为所有用户写入/读取文件夹。
  • Use other folder locations (SharePoint, local drive, network drive) to upload the file to. 使用其他文件夹位置(SharePoint,本地驱动器,网络驱动器)将文件上传到。

Any idea on how to get this problem to work? 关于如何解决此问题的任何想法吗? Any prospective on how to debug the problem is also appreciated. 任何对如何调试问题的准见解也将受到赞赏。

Update: It is probably being my account is set as the app pool account in IIS. 更新:可能是我的帐户被设置为IIS中的应用程序池帐户。 I am still not sure what the issue is. 我仍然不确定是什么问题。

This wouldn't be something like the proxy settings would it? 这不会像代理设置那样吗?

Do you need to add the default proxy setting to your 您是否需要将默认代理设置添加到您的

WebRequest 

Also do you need to add 还需要添加

<system.net> 
  <defaultProxy useDefaultCredentials="true" /> 
</system.net> 

To your web config? 到您的网络配置?

This may help. 这可能会有所帮助。

How should I set the default proxy to use default credentials? 我应该如何设置默认代理使用默认凭据?

我决定尝试另一种方法,并使用此处描述的httpposted文件和.saveAs()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM