Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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# 从web.config文件为http请求配置身份验证_C#_Asp.net_Httprequest - Fatal编程技术网

C# 从web.config文件为http请求配置身份验证

C# 从web.config文件为http请求配置身份验证,c#,asp.net,httprequest,C#,Asp.net,Httprequest,所以我有一大堆从c#到web api的请求,它们需要基本的身份验证。我知道我可以做这样的事情: var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url"); String username = "abc"; String password = "123"; String encoded = Convert.ToBase64String(System.Text.Encod

所以我有一大堆从c#到web api的请求,它们需要基本的身份验证。我知道我可以做这样的事情:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");

        String username = "abc";
        String password = "123";
        String encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
        httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

但我不想为每个请求手动将凭据添加到头中。我想知道是否有一种方法可以全局验证我的所有请求(从web.config,可能类似于sql连接的ConnectionString?)。

如果您希望您的凭据是静态的,但可以更改,那么您可以继续您的想法,将用户名和密码添加到web.config应用程序设置中。请参阅

您可以创建一个实用程序方法来添加授权标头

public static void AddAuthorizationHeader(ref HttpWebRequest request)
{
    string username = //load value from web.config
    string password = //load value from web.config
    string encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
}
因此,只需将它像这样用于所有需要该标头的http web请求

HttpWebRequest request = new HttpWebRequest();
UtilityClass.AddAuthorizationHeader(ref request);
request.ContentType = "application/json";
request.Method = "POST";

另外,我正在用我的手机,很抱歉这个糟糕的回答。但我想这正是您需要的。

您可以创建一个继承HttpClient的类,并按照您的建议从web.config获取用户名和密码

public class AuthenticatedClient : HttpClient
{
    public AuthenticatedClient()
    {
        string password = ConfigurationManager.AppSettings["Password"];
        string userName = ConfigurationManager.AppSettings["UserName"];
        string encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));

        BaseAddress = new Uri("http://url");
        DefaultRequestHeaders.Add("Authorization", "Basic " + encoded);
        DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }
}
<appSettings>
    <add key="UserName" value="abc" />
    <add key="Password" value="123" />
</appSettings>
在web.config中

public class AuthenticatedClient : HttpClient
{
    public AuthenticatedClient()
    {
        string password = ConfigurationManager.AppSettings["Password"];
        string userName = ConfigurationManager.AppSettings["UserName"];
        string encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));

        BaseAddress = new Uri("http://url");
        DefaultRequestHeaders.Add("Authorization", "Basic " + encoded);
        DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }
}
<appSettings>
    <add key="UserName" value="abc" />
    <add key="Password" value="123" />
</appSettings>

您可以随时添加全局过滤器,或者手动将其添加到您需要添加的任何特定方法之上,但这并不完全是我想要的,而是一个有趣的解决方案。塔克斯!