Reputation: 67
I have an ASP.NET application, which use some files in project directory (license, logs, etc). And I need method to check for file permissions. I wrote this one:
public static bool IsCurrentUserHaveAccessToFile(string filePath,
FileSystemRights accessType)
{
WindowsIdentity currentUser = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(currentUser);
FileSecurity security = File.GetAccessControl(filePath);
var rights = security.GetAccessRules(true, true, typeof(NTAccount));
foreach (FileSystemAccessRule right in rights)
{
NTAccount ntAccount = right.IdentityReference as NTAccount;
if (principal.IsInRole(ntAccount.Value) &&
right.AccessControlType == AccessControlType.Deny &&
right.FileSystemRights.Contains(accessType))
{
log.Debug(string.Format("Checking user {0} for access permissions {1} to file {2} - DENY", currentUser.Name, accessType, filePath));
return false;
}
}
log.Debug(string.Format("Checking user {0} for access permissions {1} to file {2} - ALLOW", currentUser.Name, accessType, filePath));
return true;
}
This method gets a current user and.. well, you can see it :)
The question is: it really is the user that is used to access the file? If IIS uses some other user different from the current one, how can I programmatically get it?
Upvotes: 1
Views: 111