C#授予"作为服务登录"允许Windows用户 [英] c# Granting "Log On As Service" permission to a windows user
问题描述
我如何授予用户对服务的LogOnAsService吧?
how do I grant a user the LogOnAsService right for a service?
我需要手动执行此操作,在 SERVICES.MSC 的程序,我可以去服务,更改密码(设置相同,有前),点击Apply,我得到一个消息
I need to do this manually, in the services.msc app I can go to the service, change the password (setting the same that there was before), click apply and I get a message:
帐户。\ Postgres的已经 授予作为服务登录的权利。
The account .\postgres has been granted the Log On As Service right.
我如何做到这一点从code,否则我有我每次运行应用程序的时候给此权限的手,这是不是一种可能性
How do I do this from code, because otherwise I have to give this permission by hand each time I run the application and this is not a possibility
@Steve
static void Main()
{
// irrelevant stuff
GrantLogonAsServiceRight("postgres");
// irrelevant stuff
}
private static void GrantLogonAsServiceRight(string username)
{
using (LsaWrapper lsa = new LsaWrapper())
{
lsa.AddPrivileges(username, "SeServiceLogonRight");
}
}
和LSA LIB被这家伙威利。
and the LSA lib by this guy Willy.
推荐答案
见的在C#中授予用户权限。
您必须调用通过P / Invoke的LSA的API,并且该URL引用了一个包装类,做你们。因此,code你最终得到的是简单的:
You have to invoke the LSA APIs via P/Invoke, and that URL has a reference to a wrapper class that does that for you. So the code you end up with is simple:
private static void GrantLogonAsServiceRight(string username)
{
using (LsaWrapper lsa = new LsaWrapper())
{
lsa.AddPrivileges(username, "SeServiceLogonRight");
}
}
这篇关于C#授予"作为服务登录"允许Windows用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!