How to use RegOpenKeyEx in Local System app? [duplicate] - c#

This question already has answers here:
Updating HKEY_CURRENT_USER hive from a service
(2 answers)
Closed 4 years ago.
I have the problem with accessing "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run". Function returns result 2, that means "File not found". I'm pretty sure this happened because of Local System rights in application but don't know how to fix this.
int result = RegOpenKeyEx(registryHive, registrySubname, 0,STANDART_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY , out registryKey);
Any ideas?
UPD: I've checked this in application with current user rights and everything worked

When running as the LocalSystem account, such as in a service, you can't use RegOpenKeyEx() to open the HKEY_CURRENT_USER hive of any user account other than LocalSystem. To open the HKCU hive of another user, you need to first impersonate that user, such as with ImpersonateLoggedOnUser(), and then use RegOpenCurrentUser().

Related

Active Directory: Query if user is logged [duplicate]

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

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

Run as Administrator in the startup [duplicate]

This question already has answers here:
How to run a program automatically as admin on Windows 7 at startup?
(9 answers)
Closed 9 years ago.
hey I'm trying to launch an app on the startup I have always done it just fine by using this code :
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rkApp.GetValue("Folder Locker") == null)
{
rkApp.SetValue("Folder Locker", Application.ExecutablePath.ToString());
}
But now when I do this:
requestedExecutionLevel level="requireAdministrator" uiAccess="false"
to start as administrator the app doesn't start on the start up I need help I hope it would be a small problem and not a big deal.
You cannot get an application to run at logon that will elevate and somehow bypass the UAC dialog. That would pretty much defeat the purpose of UAC.
Your options include:
Accepting that the user will be prompted for elevation.
Modifying your application so that it does not require elevation. If some operations require elevation, then start a new elevated process to perform those tasks
Running your process as service in session 0 where UAC does not apply.

copying file to a directory(c: or c:program files) that i dont have permission [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Allow access permission to write in Program Files of Windows 7
okay, here is my code :
System.IO.File.WriteAllBytes(path1, path2);
however, there is a problem.
user(windows 7 users dont have permission to copy file to c: by default)(...i mean for programs...)
therefore, program fails.
if user gives permission to copy file in c: program works fine. but you cant tell every user " go to permission.........." so how can i make something to allow user that i copy my file to c://
You may add the Application Manifest File in your project by Selection Add New Item. Update the requestedExecutionLevel to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
This way user will be asked that the application needs to run in Administrative mode. Hope this help.
Your application shouldn't be writing to the C drive. Only users with permission, or administrators, can grant an application to do that. Instead, try writing to folders that aren't limited by tight security. You can see some of them here.
You can't. You have to give the user permissions.

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).

Categories