开发者

Getting Active Directory current User with Windows Service

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,


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜