开发者

How to get Current User credentials in windows 7 with a Windows Service?

开发者 https://www.devze.com 2023-03-23 06:13 出处:网络
I\'m trying to get开发者_运维知识库 currently user credentials with a windows service in Windows 7.

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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消