使用 Windows 服务获取 Active Directory 当前用户

发布于 2024-11-27 06:12:24 字数 1263 浏览 0 评论 0原文

首先,一位名叫“leppie”的好心用户试图帮助我,但我无法得到我正在寻找的答案,而且这是一件紧急的事情。

我使用 LocalSystem 帐户在 Windows 7 中运行一个 Windows 服务(由于这个 win 服务将远程且静默地安装在许多计算机上,我想我需要通过以下代码在 ServiceInstaller.Designer.cs 中使用 LocalSystem:

this.ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; 
this.ProcessInstaller.Password = null; 
this.ProcessInstaller.Username = null;

当我运行此 Windows 服务时下面的代码无法获取当前登录用户的凭据(用户没有管理员权限,甚至我自己也没有

using (DirectoryEntry de = new DirectoryEntry("LDAP://MyDomainName"))
{
    using (DirectorySearcher adSearch = new DirectorySearcher(de))
    {
        adSearch.Filter = "(sAMAccountName=" + Environment.UserName + ")";
        SearchResult adSearchResult = adSearch.FindOne();

        UserInternalEmail = GetProperty(adSearchResult, "mail");
    }
}

this.ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.<User ? LocalService ? NetworkService>; 
this.ProcessInstaller.Password = "adminpassword"; 
this.ProcessInstaller.Username = "adminusername";

。 ,让我们说一个ABC 用户是管理员,假设我知道该 ABC 管理员的密码和用户名,但是当该管理员更改密码时,我认为这会影响我将在 70 台计算机上运行的 winservice

有没有办法检索用户 。活动目录上的凭据?如果您为我提供一些代码示例,我将不胜感激。

非常感谢,

First of all, a kind user named "leppie" tried to help me but I couldn't get the answer I am looking for and it's kind of an urgent matter.

I run a windows service in Windows 7 with LocalSystem account (Since this win service will be installed many computers remotely and silently, I guess I need to use LocalSystem in ServiceInstaller.Designer.cs by the code below:

this.ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; 
this.ProcessInstaller.Password = null; 
this.ProcessInstaller.Username = null;

When I run this windows service the code below cannot get the currently logged in user's credentials (the users do not have admin privileges, not even myself).

using (DirectoryEntry de = new DirectoryEntry("LDAP://MyDomainName"))
{
    using (DirectorySearcher adSearch = new DirectorySearcher(de))
    {
        adSearch.Filter = "(sAMAccountName=" + Environment.UserName + ")";
        SearchResult adSearchResult = adSearch.FindOne();

        UserInternalEmail = GetProperty(adSearchResult, "mail");
    }
}

I have been suggested to run the WinService under a AD/LDAP/domain account, but which user could this be?

this.ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.<User ? LocalService ? NetworkService>; 
this.ProcessInstaller.Password = "adminpassword"; 
this.ProcessInstaller.Username = "adminusername";

I mean, lets say an ABC user is an admin and lets say I knew the password and username of this ABC admin, but when this admin changes the password, I think this will effect my winservice which will be running on 70 computers.

Is there a way to retrieve the user credentials on active directory? I would be really appreciated if you provide me some code samples..

Thank you very very much,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2024-12-04 06:12:24

问题是,Environment.UserName 将始终返回运行服务的服务帐户的用户名,而不是登录到计算机的用户。

请参阅此问题了解如何获取登录到工作站的用户名。请记住,Windows 将允许多个用户同时登录。

The problem is that Environment.UserName will always return the username of the service account under which the service is running, not the user logged into the machine.

See this question for information on how to get the names of users logged into the workstation. Keep in mind that Windows will allow multiple users to be logged in at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文