简体   繁体   中英

Which user IIS uses for access to files

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?

Depends on where your code is hosted. This may be of help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM