简体   繁体   English

如何在不使用P / Invoke或反射的情况下在.NET中启用SeCreateGlobalPrivilege?

[英]How to enable the SeCreateGlobalPrivilege in .NET without resorting to P/Invoke or reflection?

My net.pipe WCF binding does not work when the server runs non elevated. 当服务器运行非提升时,我的net.pipe WCF绑定不起作用。 This blog helped me find the answer - http://weblogs.thinktecture.com/cweyer/2007/12/dealing-with-os-privilege-issues-in-wcf-named-pipes-scenarios.html 这个博客帮助我找到答案 - http://weblogs.thinktecture.com/cweyer/2007/12/dealing-with-os-privilege-issues-in-wcf-named-pipes-scenarios.html

So, I added the SeCreateGlobalPrivilege privilege to the relevant non admin user, but now I have to enable the privilege programmatically in .NET 因此,我将SeCreateGlobalPrivilege权限添加到相关的非管理员用户,但现在我必须在.NET中以编程方式启用该权限

Now, there are several examples on the internet how to do it in .NET, but all of them essentially rewrite plain C code to C# with P/Invoke. 现在,互联网上有几个例子如何在.NET中实现它,但它们都基本上用C / Invoke将普通C代码重写为C#。

I wish to know how to do it using the dedicated .NET system types, like ObjectSecurity, Privilege and so on, so that my code does not do any P/Invoke - let the system code do it for me. 我想知道如何使用专用的.NET系统类型,如ObjectSecurity,Privilege等,以便我的代码不做任何P / Invoke - 让系统代码为我做。

Is it possible? 可能吗?

Thanks. 谢谢。

EDIT1 EDIT1

What makes me think it is possible? 是什么让我觉得有可能? Well, I searched for the AdjustTokenPrivileges P/Inovke API usage in .NET using Reflector and found, that there is ObjectSecurity.Persist method , which ultimately invokes this P/Invoke. 好吧,我使用Reflector搜索.NET中的AdjustTokenPrivileges P / Inovke API用法,发现有ObjectSecurity.Persist方法,最终调用此P / Invoke。 Next, this ObjectSecurity has one protected constructor, meaning it is possible for a non MS code to derive from it and invoke this method. 接下来,此ObjectSecurity具有一个受保护的构造函数,这意味着非MS代码可以从中派生并调用此方法。

So, it seems feasible using type-safe .NET code (ie no reflection). 因此,使用类型安全的.NET代码(即没有反射)似乎是可行的。

If you don't want to use P/Invoke by yourself, you can still use the internal System.Security.AccessControl.Privilege class, using Reflection mechanisms, like this: 如果您不想自己使用P / Invoke,您仍然可以使用内部System.Security.AccessControl.Privilege类,使用Reflection机制,如下所示:

    // => privilege = new Privilege("SeCreateGlobalPrivilege");
    Type privilegeType = Type.GetType("System.Security.AccessControl.Privilege");
    object privilege = Activator.CreateInstance(privilegeType, "SeCreateGlobalPrivilege");

    // => privilege.Enable();
    privilegeType.GetMethod("Enable").Invoke(privilege, null);

    // =>  privilege.Revert();
    privilegeType.GetMethod("Revert").Invoke(privilege, null);

I'm not sure it's really better, because it's more or less a hack, not supported et al., but ok, it's easy for lazy guys :-) 我不确定它真的更好,因为它或多或少是一个黑客,不支持等等,但好吧,这对懒人来说很容易:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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