WCF 模拟不是模拟管理员

Posted

技术标签:

【中文标题】WCF 模拟不是模拟管理员【英文标题】:WCF impersonation is not impersonating an administrator 【发布时间】: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 机器。

有什么建议吗?

【问题讨论】:

【参考方案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 我问了一个相关问题,请您检查一下:***.com/questions/18842970/…

以上是关于WCF 模拟不是模拟管理员的主要内容,如果未能解决你的问题,请参考以下文章

在 Jest 中模拟 Firebase 管理员时出错:“TypeError:admin.firestore 不是函数”

ASP.NET 中的 WCF 模拟异常

自托管 WCF 中的模拟?

在模拟上下文之外调用 WCF 服务?

一个简单的主机管理模拟程序

WCF 模拟和 SQL 可信连接?