Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改自己根服务器中的目录,以便通过NetworkCredential(用户名、密码)进行访问_C#_.htaccess_Networkcredentials - Fatal编程技术网

C# 如何更改自己根服务器中的目录,以便通过NetworkCredential(用户名、密码)进行访问

C# 如何更改自己根服务器中的目录,以便通过NetworkCredential(用户名、密码)进行访问,c#,.htaccess,networkcredentials,C#,.htaccess,Networkcredentials,我收到错误401,未经授权的访问 来自Webclient的错误消息: 远程服务器返回错误:(401)未授权 如果我想通过c#webclient类访问目录。 问题:目录授权失败,连接正常,目录中的文件列表也已显示 我已经在提供商的浏览器界面上保存了目录。 我还测试了.htaccess,用用户名和密码保护这个目录,没有成功 也许我做错了什么,但是代码必须是好的,因为如果我添加 选项+索引 仅访问我前面提到的.htaccess,即可获取此目录中的文件列表。但是如果我想下载这些文件,就会出现上面的错误(

我收到错误401,未经授权的访问

来自Webclient的错误消息:

远程服务器返回错误:(401)未授权

如果我想通过c#webclient类访问目录。 问题:目录授权失败,连接正常,目录中的文件列表也已显示

我已经在提供商的浏览器界面上保存了目录。 我还测试了.htaccess,用用户名和密码保护这个目录,没有成功

也许我做错了什么,但是代码必须是好的,因为如果我添加 选项+索引 仅访问我前面提到的.htaccess,即可获取此目录中的文件列表。但是如果我想下载这些文件,就会出现上面的错误(401次未经授权的访问)

你知道我如何使用用户名和密码通过C#webclient从这个目录下载文件来更改对我的目录的访问权限吗

我的代码:

Client.UseDefaultCredentials = true;
Client.Credentials = new NetworkCredential("user", "pw");

Directory.CreateDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "DownloadArchive");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url/directory");
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string html = reader.ReadToEnd();
                Regex regex = new Regex(GetDirectoryListingRegexForUrl("url/directory"));
                MatchCollection matches = regex.Matches(html);
                if (matches.Count > 0)
                {
                    foreach (Match match in matches)
                    {
                        if (match.Success)
                        {
                            Client.DownloadFile("url/directory" + match.Groups["name"], 
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\" + "DownloadArchive" + Path.DirectorySeparatorChar + 
match.Groups["name"]);
                            writer.Log("File " + match.Groups["name"] + " downloaded into " + 
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "DownloadArchive");
                        }
                    }
                }
            }
        }
.htaccess,用于读取目录中的所有文件名:

Options +Indexes
.htaccess,我尝试添加用户名和密码:

AuthType Basic
AuthUserFile /root/.passwd
AuthName "HTACCESS TEST"
order deny,allow
allow from all
require valid-user
目录根中的.passwd,是通过根服务器上的putty使用htpasswd程序创建的:

UserName:{SHA}...........................

用德语发布错误信息基本上和根本不发布错误信息一样有用,因为这是一个英语社区。@Camiloteriento我翻译了它:)我只是照原样放了。。。如果你读得更深入,你就会看到翻译。用德语写错误信息基本上和根本不发布错误信息一样有用,因为这是一个英语社区。@Camiloteriento我翻译了它:)我只是照原样写。。。如果你读得更深入,你就会看到翻译。