【问题标题】:WCF impersonation is not impersonating an administratorWCF 模拟不是模拟管理员
【发布时间】:2010-10-07 16:54:50
【问题描述】:

我正在尝试使用 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.identity 都是要模拟的正确用户。并且该用户是管理员用户组的成员。

我认为这与在 windows vista 及更高版本中实现的 UAC 有关。这将是一个问题,因为这将要运行的生产机器是一个 win2k8-r2 机器。

有什么建议吗?

【问题讨论】:

    标签: c# wcf uac impersonation


    【解决方案1】:

    看看这个article,在“Coping with Windows Vista”部分下,一篇写得很好的关于 UAC 和以编程方式检查 Admin privs 的文章。

    【讨论】:

    • 呃,我很担心这样的事情。有没有更简单的方法来检查用户是否是管理员?
    【解决方案2】:

    由于我不想做所有这些工作(来自 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 我问了一个相关问题,请您检查一下:stackoverflow.com/questions/18842970/…
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2020-08-24
    • 2011-02-20
    相关资源
    最近更新 更多