Check user permissions [duplicate] - c#

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

Related

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

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

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

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

Access denied error

I am trying to delete the excel file from a specipic location . but can't deleting. having error :
Access to the path 'C:\mypath\sample.xlsx' is denied.
I write a code as :
protected void imgbtnImport_Click(object sender, ImageClickEventArgs e)
{
try
{
string strApplicationPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
string strXLStoredDirectoryPath = strApplicationPath + "/Information Documents/";
DirectoryInfo di = new DirectoryInfo(strXLStoredDirectoryPath);
string fileName = flUpldSelectFile.FileName;
if (!File.Exists(strXLStoredDirectoryPath))
{
Directory.CreateDirectory(strXLStoredDirectoryPath);
di.Attributes = FileAttributes.Normal;
}
string strCreateXLFileDestinationPath = strXLStoredDirectoryPath + fileName;
if (File.Exists(strCreateXLFileDestinationPath))
{
File.Delete(strCreateXLFileDestinationPath);
}
flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath);
di.Attributes = FileAttributes.ReadOnly;
}
catch (Exception)
{
throw;
}
}
please guide.........
-***********************************************************************
Still problem there . it is not resolved . getting UnauthorizedAccessException. as access denied to deleting file. I m tired now . please help; I tried many things..please help
-***********************************************************************
Is may be iffect of VSS ? i am using that
UPDATE:
Part of your issue might be what is saving/creating this file. If you're using a built in "Save" or "SaveAs" feature the underlying file stream might still have a lock on the file. writing your own save logic with a FileStream wrapped in a Using statement will help dispose the stream right when you're done thus allowing you to further manipulate the file within the same context.
if flUpldSelectFile.SaveAs(strCreateXLFileDestinationPath); is the only logic that saves the file then get rid of the built in SaveAs functionality. write your own save logic using a FileStream wrapped in a Using block.
In your example i can't see what flUpldSelectFile is so i am assuming it is a System.Web.UI.WebControls.FileUpload control. Here is an example of rolling your own save logic.
using (FileStream fs = new FileStream(strCreateXLFileDestinationPath, FileMode.Create))
{
byte[] buffer = flUpldSelectFile.FileBytes;
fs.Write(buffer, 0, buffer.Length);
}
As stated previously, use this tool to find out if there is a lock on the file by another process.
ORIGINAL
Pop open this wonderful tool and search for that file to see who/what has it locked
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
(source: microsoft.com)
If your code is working under IIS , Note that ASPNET user doesn't have access to computer files, you should give access to it, that is not recommended, or store you files in the place where ASPNET user have access
see here
Try a combination of these 2 steps:
Set the IIS application pool to run under an account with privileges such as a domain account or local user account (not a default account like local service or local system). Instructions for IIS7.
Turn impersonation on in the web.config file, in the <system.web> section:
<identity impersonate="true"/>
<identity impersonate="true" userName="contoso\Jane" password="password"/>
I think the message is clear, you do not have authorization to delete the file or it is opened by another application. I bet 2$ you can't delete the file manually either.
As others have said, this is because IIS runs your application as a user with restricted access rights. This is a wise security precaution, so that your system is less vulnerable to malicious attacks.
What you need to do is to give the ASPNET user access to the specific folder. You do that from the security tab in the properties of a folder. The user you need to give full control to depends on the version of IIS you are using. In Windows XP it is ASPNET. In Windows Server 2003, 2008 and Windows Vista, 7 it is NETWORK_SERVICE.
See also this question for more details.
Make sure the file isn't opened or
locked by another user/process.
Make sure ASPNET user has access on the file\folder (check the file\folder's property using windows explorer and go to security tab. check if ASPNET user is added there).
One of two things are happening. Either the file is already open, or the permission of the user running IIS does not have the proper permissions.
Either way, this utility ProcMon: Proc Mon
will help you determine the issue. Run ProcMon, kick off your process to try and delete the file. Then go back to procmon. Hit Ctrl-E to turn off the capture, then Ctrl-F to find. Enter the name of the file you're trying to delete. Then once you've found the correct line with the access denied (or similar error) Double click on the the line to get further information. When you click on the Process tab, it will show you the exact user that is trying to delete the file.
So, if it is a file permission issue, you now know the exact user, and can therefore go to the file system right click on the folder that houses the file you are trying to delete, and grant that user permissions to read/write/update that folder.
Second, if the file is locked open instead of a permissions issue, you will have to find out what process is holding open the file. If you are also writing this file in another part of your code, perhaps you are not closing it properly or releasing the object reference.
Have you verified that the file does not have the read-only attribute set?
I don't think we have enough info to be helpful. What is the security context (identity) during the call to Delete? Is the application impersonating the end user? If it is, how are they authenticated? If by Windows / Active Directory, then you'll need to verify that user's access rights to the specific file. If by Forms login, then you should probably not impersonate and verify that the AppPool's security context has the appropriate access rights.

Categories