繁体   English   中英

WCF模拟不是冒充管理员

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM