私のASP.NETWebアプリケーションは、イントラネットでWindows認証を使用しています。同じドメイン上の別のサーバーに対してサーバー側のhttpリクエストを実行できるようにしたいのですが、これにもWindows認証が必要です。
ここで追加のリクエストを行うときに、認証されたユーザーを一時的に偽装する手順に従いました。
http://msdn.microsoft.com/en-us/library/ff647404.aspx
次のようなコードを使用します。
using System.Security.Principal;
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
// Start impersonating
ctx = winId.Impersonate();
// Now impersonating
// Access resources using the identity of the authenticated user
var request = WebRequest.Create("http://intranet/secureapp");
request.Credentials = CredentialCache.DefaultCredentials;
var response = request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
Response.Write(streamReader.ReadToEnd());
}
}
// Prevent exceptions from propagating
catch
{
}
finally
{
// Revert impersonation
if (ctx != null)
ctx.Undo();
}
// Back to running under the default ASP.NET process identity
しかし、残念ながら、私は常に401の不正なエラーを受け取ります。
Webサーバーをアクティブディレクトリで構成して、認証されたユーザーを委任できるようにする必要がありますか(約200人のユーザーのいずれかである可能性があるため、200回何もする必要はありません:))?もしそうなら、誰かがこれを行う方法を教えてもらえますか?