Active Directory: Query if user is logged [duplicate] - c#

This question already has an answer here:
Active Directory check if user is logged in
(1 answer)
Closed 8 years ago.
I'm trying to query a LDAP server to get information if an user is logged in or not.
In a collection of properties (from return) none tell me specifically the information, but there are two in particullar that can do this: lastlogon and lastlogoff.
But lastlogoff it is always 0.
: /
How I can get the real value of lastlogoff?

May I refer you to this article on SO? I used this solution before, because as noted, there was no method of finding this information within AD. One could seek out the computer names on the network, but this does not indicate the actual user.
Active Directory check if user is logged in
I hope this is helpful to you, i used this from the linked article....
"...Another alternative could be to craft a logon/logoff script that
writes to a particular file/database and you could monitor that file
to see who is logged in...."
Then, this exmaple is for vbscript and WMI. As stated, a third party tool available through sysinternals exists.
You said you have no access to change LDAP server. Does this include logon/logoff scripts?
Here is the link: http://blogs.msdn.com/b/alejacma/archive/2008/03/04/how-to-get-the-logged-on-user-with-wmi-vbscript.aspx

Related

How to get the computer name of a person

Is it possible to get the computer name for a person that is found through Active Directory?
I am currently getting the person via DirectorySearcher object, and from there can get the user's SID, but i don't know where to look next in order to find the actual computer name that is assigned to that SID or person?
Or maybe there is an easier way to get this information from Active Directory?
There is no association of computers to users in default AD - so there is no way to get such information.
You may find last/currently logged on set of machines for a user (also I don't think this is available in AD, see How to find what user last logged onto a given computer through Active Directory in C#? for some starting points) or maybe you add custom information that gives that association in AD (Retrieving custom Active Directory properties of users).

Check user permissions [duplicate]

This question already has answers here:
Checking file/folder access permission
(3 answers)
Closed 8 years ago.
I have an Application running in a server, that takes a username and file path. The idea to check if the user can read the file (the target user is not the same user running the program).
So how to check read permissions for specific user ??
I can't take responsibility for this as I googled it and the answer was by James Newton-King found here- How to present credentials in order to open file?
You want to impersonate a user who does have the rights to access the file.
I recommend using a class like this - http://www.codeproject.com/KB/cs/zetaimpersonator.aspx. It hides all the nasty implementation of doing impersonation.
using (new Impersonator("myUsername", "myDomainname", "myPassword"))
{
string fileText = File.ReadAllText("c:\test.txt");
Console.WriteLine(fileText);
}
Check this Documentation, this might be useful:
http://msdn.microsoft.com/en-us/library/system.io.file.getattributes(v=vs.110).aspx

Can Domain name and host computer name be same

Recently I was trying to find answer to question "How to determine if current logged in user is part of domain or not"
On Stackoverflow I found pretty decent answers for the above which involved usage of
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
But I also see a simple solution saying
System.Environment.UserDomainName; - gives name of the current user's domain name.
System.Environment.MachineName; - gives name of the machine.
If above two are not the same means the user is part of domain returned by UserDomainName
Fair enough, also confirmed by this link from MSDN
Now the question is what if the "Domain name" and "Machine name" are identical?
or
Is it possible that a machine with the exact same name as domain name can join the domain?
If this is true, is there any possibilities to counter check this by adding something to this simple solution
I bet you the current logged on user does not have a distingished name if he or she isn't logged on to a domain. Use GetUserNameEx with the EXTENDED_NAME_FORMAT type NameFullyQualifiedDN (1).
If you get a value back then you know the current user is logged on to a domain. Though, Calling this function might block your program unexpectedly for a period of time. You probably want to call this function in a asynchronous fashion as it might atempt to connect to some Active Directory service and just eventually fail or timeout.
An yet more resilient way to accomplish this would be to look up the account SID and check whether the EqualPrefixSid can compare it with the machine. If this is the case, then you know the current account is local to the machine (i.e. not in a domain). This does not involve crossing the network and waiting for a timeout, so it's a more direct approach. Interestingly, it also implies that if the machine name is the same as the user domain, Windows would have to assume that the domain you want to logon on to is the local machine and it should therefore be almost impossible to logon to the actual domain if the machine name really is the same as the domain.

C# - How do I get the "Everybody" user? [duplicate]

This question already has an answer here:
How to get the IdentityReference for "Everyone" to create MutexAccessRule on localized systems?
(1 answer)
Closed 9 years ago.
I already wrote a code which can create a share and change permissions for the current user. The goal was to always allow all for everybody on share level and deny rights on ntfs acl level.
I use a german windows and I noticed that I only can access the everybody user by using "jeder". In english it would be the user "everybody" or "all" I think?! Anyway I'm searching for a way to get the name of the everybody user language independent.
Hope this is possible.
The name that the English version of Windows uses is "Everyone".
You can get the user regardless of language by using the WellKnownSidType.WorldSid value:
var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var account = (NTAccount) sid.Translate(typeof(NTAccount));
I believe the way to do it has been answered here: How can I get the local group name for guests/administrators ?
This "everyone" SID is a well known SID "S-1-1-0" (the list is availalble here: Well-known security identifiers in Windows operating systems).

Know if app is installed for "All users"

How do detect if my app was installed for "All users" or just for one user.
Today I check for files put in either Environment.SpecialFolder.CommonApplicationData or Environment.SpecialFolder.ApplicationData by my installer.
Is there a better way?
To expand on Sachin Gaur's answer:
The S-1-5-18(*) folder is for Local System user - which is used when installing for All Users.
Intallation for the current user would have the product key in a folder named after that user's security identifier (S-something-else).
To get the current user's SID, use System.Security.Principal.WindowsIdentity.GetCurrent() to get a WindowsIdentity. Then, use the User property to get a SecurityIdentifier. Then use ToString() to get the string value.
See MSDN for the GetValue method used to access the registry.
(*) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\
I hope this may be useful to you.
You can know whether application is installed for all user or not by reading the registry value. For this, you must know the product code of the application:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\
To read the registry entry in C#, you can use GetValue() method of Registry class in Microsoft.Win32.

Categories