[英]WCF impersonation is not impersonating an administrator
我正在嘗試使用WCF來做一些遠程用戶管理的事情。 我重復使用我在服務器2003盒子上的一些代碼並且工作正常,但是當我檢查調用該函數的用戶是否為管理員時,在我的Windows 7測試框中它說它不是。
[OperationBehavior(Impersonation=ImpersonationOption.Required)]
public string SetPassword(string username)
{
WindowsPrincipal principal = new WindowsPrincipal(OperationContext.Current.ServiceSecurityContext.WindowsIdentity);
System.Diagnostics.Debug.Print(WindowsIdentity.GetCurrent().Name);
System.Diagnostics.Debug.Print(principal.Identity.Name);
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
//try
{
lock (Watchdog.m_principalContext)
{
using (UserPrincipal up = UserPrincipal.FindByIdentity(Watchdog.m_principalContext, username))
{
string newpassword = CreateRandomPassword();
up.SetPassword(newpassword);
up.Save();
return newpassword;
}
}
}
//catch
{
return null;
}
}
else
throw new System.Security.SecurityException("User not administrator");
}
principal.IsInRole(WindowsBuiltInRole.Administrator)
每次都返回false。 我當前的身份和principal.idenity都是被模仿的正確用戶。 該用戶是管理員用戶組的成員。
我認為它與在Windows Vista中實現的UAC有關。 這將是一個問題,因為這將是一個win2k8-r2框的生產機器。
有關該怎么辦的任何建議?
看一下這篇文章 ,在“應對Windows Vista”一節中,這是一篇關於UAC的非常好的文章,並以編程方式檢查Admin privs。
由於我不想做所有工作(來自RandomNoob的帖子)以檢查用戶是否是管理員並且服務已經在管理上下文中運行,我決定放棄模擬。 我創建了一個名為WCFUsers的新用戶組,任何將使用該服務的用戶都被添加到該組中。 它現在在自己的上下文中執行System.DirectoryServices.AccountManagement
操作。
[OperationBehavior(Impersonation=ImpersonationOption.NotAllowed)]
public string SetPassword(string username)
{
WindowsPrincipal principal = new WindowsPrincipal(OperationContext.Current.ServiceSecurityContext.WindowsIdentity);
if (principal.IsInRole("WCFUsers"))
{
try
{
lock (Watchdog.m_principalContext)
{
using (UserPrincipal up = UserPrincipal.FindByIdentity(Watchdog.m_principalContext, username))
{
string newpassword = CreateRandomPassword();
up.SetPassword(newpassword);
up.Save();
return newpassword;
}
}
}
catch
{
return null;
}
}
else
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.