HTTP基本認証を要求するサーバーによってWWW-Authenticateヘッダーで送信されたRealmプロパティを読み取るにはどうすればよいですか?
3587 次
2 に答える
8
この質問に関して反対票を投じた人が実際に抱えている問題が何であるかはよくわかりません。
基本認証領域を含む WWW-Authenticate ヘッダーを取得する大まかなコードを次に示します。ヘッダーから実際のレルム値を抽出することは演習として残されていますが、非常に簡単なはずです (正規表現を使用するなど)。
public static string GetRealm(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
try
{
using (request.GetResponse())
{
return null;
}
}
catch (WebException e)
{
if (e.Response == null) return null;
var auth = e.Response.Headers[HttpResponseHeader.WwwAuthenticate];
if (auth == null) return null;
// Example auth value:
// Basic realm="Some realm"
return ...Extract the value of "realm" here (with a regex perhaps)...
}
}
于 2011-12-08T22:15:21.983 に答える