私のアプリケーションは IIS 7.0 で実行されています。認証されたユーザーを偽装し、他のユーザー アカウントのロックを解除またはリセットすることになっています。ワークステーションで開発していたときは問題なく動作しましたが、サーバーにアップロードすると偽装が機能しなくなり、AD オブジェクトにバインドされず、同じ例外がスローされ続けます。私は以前に PrincipalContext を使用して同じ問題を抱えていましたがusing(HostingEnvironment.Impersonate())
、そのアクションに認証されたユーザーを必要としなかったため、それを回避することができました。しかし、今はそうしているので、その回避策は使用できません。この問題の実際の修正が必要です。ご意見をいただければ幸いです。私は問題の解決策を広範囲に探してきましたが、これまでのところどれも機能していません。例外をスローし続ける私が使用しているコードは次のとおりです。
using (DirectorySearcher search = new DirectorySearcher(directoryEntries[counter]))
{
//Sets the filter to find a user object with the target user username
search.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
//Sets the searchscope to search the whole AD
search.SearchScope = SearchScope.Subtree;
//Executes the search for one result and stores it in result.
SearchResult result = search.FindOne();
//Creates a directory entry from the result.
using (DirectoryEntry targetUser = result.GetDirectoryEntry())
{
//This if-else statement checks if the user is locked, if it is then
//the unlock is performed, and the unlockPerformed variable is set to
//true, if it isn't then unlockPerformed is set to false.
if (Convert.ToBoolean(targetUser.InvokeGet("IsAccountLocked")))
{
targetUser.InvokeSet("IsAccountLocked", false);
targetUser.CommitChanges();
unlockPerformed = true;
}
else
{
unlockPerformed = false;
}
}
}
このコードは、アップロードする前に完全に機能しました。提案があれば大歓迎です。これを監視して、できるだけ早く修正できるようにします。前もって感謝します。
更新: 問題を修正しました
この記事によると、リモート マシンではなくホスト マシンから実行されているプログラムは、実際には非常にわかりやすい症状です。 .aspx . その記事によると、問題は、偽装設定が偽装に設定されていたため、この動作が発生したことであり、DELEGATION が必要でした。これを行うために、このページを使用して、委任と偽装のさまざまな方法に関する情報を取得しました。「元の呼び出し元を一時的に偽装する」セクションを使用しました。
web.config ファイル内:
<identity impersonate="false"/>
これが false に設定されている場合、すべてのアクションでユーザーになりすまそうとします。これは、私が経験していたような問題を引き起こす可能性があり、私が達成しようとしていたものではありません。
コード内:
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
}
// Prevent exceptions from propagating
catch
{
}
finally
{
// Revert impersonation
if (ctx != null)
ctx.Undo();
}
// Back to running under the default ASP.NET process identity
この修正は非常に簡単ですが、このトピックに関する適切で明確な情報を見つけることはほとんど不可能です.いつか誰かがこれを役に立つと思うことを願っています.