Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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# 访问RDS时出现未经授权的错误401_C#_Asp.net_.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 访问RDS时出现未经授权的错误401

C# 访问RDS时出现未经授权的错误401,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,我正在尝试使用该代码登录到RDS,并获取身份验证cookie以进行进一步操作。当我在浏览器上运行URL时,它会要求输入用户名和密码,在成功输入用户名和密码后,我会得到包含身份验证cookie的文件。 我试图通过代码实现同样的功能,但即使在输入以下正确的详细信息后,仍会出现未经授权的错误401,这是我的代码: private string GetFormsAuthenticationCookie(string connectionUrl, NetworkCredential networkCred

我正在尝试使用该代码登录到RDS,并获取身份验证cookie以进行进一步操作。当我在浏览器上运行URL时,它会要求输入用户名和密码,在成功输入用户名和密码后,我会得到包含身份验证cookie的文件。 我试图通过代码实现同样的功能,但即使在输入以下正确的详细信息后,仍会出现未经授权的错误401,这是我的代码:

private string GetFormsAuthenticationCookie(string connectionUrl, NetworkCredential networkCredential)
        {   // // Request connection page is protected by Forms Authentication Cookie. So making a request to that page will be redirected to login page 
            // The login page is // 
            
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(connectionUrl);
            //Added this line for ceritifcate error
            httpWebRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
            CredentialCache credentialCache = new CredentialCache();
            credentialCache.Add(new Uri(connectionUrl), "Basic", networkCredential);
            httpWebRequest.PreAuthenticate = true;
            httpWebRequest.Credentials = credentialCache; 
            httpWebRequest.AllowAutoRedirect = true;
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            if (httpWebResponse.StatusCode == HttpStatusCode.OK)
            {
                //Pass
            }
            else
            {
                //Fail
            }
            string formsAuthenticationCookie;
            using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
            {
                formsAuthenticationCookie = streamReader.ReadToEnd();
                streamReader.Close();
            }
            return formsAuthenticationCookie;
        }