2010-10-07 43 views
3

我想使用WCF做一些遠程用戶管理的事情。我重複使用了我在server 2003上的一些代碼,並且運行良好,但是在我的Windows 7測試框中,當我查看調用該函數的用戶是否是管理員時,它說這不是。WCF模擬不模仿管理員

[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。我目前的身份和主要身份都是被冒充的正確用戶。並且該用戶是管理員用戶組的成員。

我認爲它與在Windows Vista和Windows Vista中實現的UAC有關。這將是一個問題,因爲這將會是一個win2k8-r2盒子。

關於怎麼做的任何建議?

回答

0

因爲我不想做所有這些工作(來自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; 
} 
+0

您是否在操作級別使用基於屬性的權限需求進行了調查? [PrincipalPermission(SecurityAction.Demand,角色= 「管理員」)] – kd7 2010-10-07 18:14:37

+0

@Scott Chamberlin我問一個相關的問題,請你檢查一下:http://stackoverflow.com/questions/18842970/asp-net-imperonate-in- netframework -2- VS-netframework -4- – 2013-09-17 07:26:10

3

看看這個article,在「應對Windows Vista」一節,這是一篇關於UAC和編程檢查Admin privs的文章。

+0

呃,我很擔心這樣的事情。有沒有更簡單的方法來檢查用戶是否是管理員? – 2010-10-07 17:21:36