C# WCF模拟未模拟管理员

C# WCF模拟未模拟管理员,c#,wcf,uac,impersonation,C#,Wcf,Uac,Impersonation,我正在尝试使用WCF做一些远程用户管理的事情。我重新使用了Server2003上的一些代码,效果很好,但在我的Windows7测试框中,当我检查调用该函数的用户是否是管理员时,它会说不是 [OperationBehavior(Impersonation=ImpersonationOption.Required)] public string SetPassword(string username) { WindowsPrincipal principal = new WindowsPri

我正在尝试使用WCF做一些远程用户管理的事情。我重新使用了Server2003上的一些代码,效果很好,但在我的Windows7测试框中,当我检查调用该函数的用户是否是管理员时,它会说不是

[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都是要模拟的正确用户。并且该用户是administrators用户组的成员

我认为这与在WindowsVista和更高版本中实现的UAC有关。这将是一个问题,因为将要使用的生产机器是win2k8-r2机器


有什么建议吗?

看看这个,在“应对Windows Vista”一节下,这是一篇写得很好的关于UAC的文章,并通过编程检查管理员权限。

因为我不想做所有这些工作(来自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;
}

呃,我很担心这样的事情。是否没有更简单的方法来检查是否有用户是管理员?您是否考虑过在操作级别使用基于属性的权限需求?[PrincipalPermission(SecurityAction.Demand,Role=“Administrators”)@Scott Chamberlin我问了一个相关问题,请检查一下: