【发布时间】:2016-03-04 12:26:31
【问题描述】:
所以我需要从 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 连接的 connectionStrings?)。
【问题讨论】:
-
您可以随时添加全局过滤器,或者在您需要添加的任何特定方法上方手动添加
标签: c# asp.net httprequest