如何使用 Windows 服务获取 Windows 7 中的当前用户凭据?
我正在尝试使用 Windows 7 中的 Windows 服务获取当前用户凭据。
当我在调试模式下运行下面的代码时(请注意,当我在调试模式下运行此代码时,winservice 在我的用户而不是本地系统上运行),没有问题。但是,当 WinService 在计算机上的 LocalSystem 帐户上运行时,当我将代码作为“已发布”(不是调试)和 Windows 服务运行时,我无法从活动目录获取当前用户?
using (DirectoryEntry de = new DirectoryEntry("LDAP://" + DomainName))
{
using (DirectorySearcher adSearch = new DirectorySearcher(de))
{
adSearch.Filter = "(sAMAccountName=" + Environment.UserName + ")";
SearchResult adSearchResult = adSearch.FindOne();
UserInternalEmail = GetProperty(adSearchResult, "mail");
}
}
提前致谢,
I'm trying to get currently user credentials with a windows service in Windows 7.
When I run the code below on DEBUG mode (notice that, when I run this on DEBUG mode, the winservice runs on my User instead of LocalSystem), there is no problem. However, when the WinService runs on LocalSystem account on the machine I cannot get current user from active directory when I run the codes as "Released" (not debug) and as Windows Service?
using (DirectoryEntry de = new DirectoryEntry("LDAP://" + DomainName))
{
using (DirectorySearcher adSearch = new DirectorySearcher(de))
{
adSearch.Filter = "(sAMAccountName=" + Environment.UserName + ")";
SearchResult adSearchResult = adSearch.FindOne();
UserInternalEmail = GetProperty(adSearchResult, "mail");
}
}
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LocalSystem 在本地系统之外没有任何权限,因此很明显 AD/LDAP 将拒绝它的查询。只需在 AD/LDAP/域帐户下运行该服务即可。
LocalSystem has no rights outside the local system, so it is pretty clear the AD/LDAP is going to reject it's query. Just run the service under a AD/LDAP/domain account.