因此,我需要從c#執行一系列的請求到web api,並且他們需要基本身份驗證。我知道我可以這樣做:爲來自web.config文件的http請求配置身份驗證
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,可能類似於connectionStrings for sql連接?)。
您可以隨時添加全局過濾器,或者添加i t手動以上任何具體的方法,你需要添加爲 – harishr