2010-10-07 46 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