We have a scenario in our client project where the client application saves and tries to fetch files from a shared folder location , which is a virtual shared folder. But we have been facing issues with a specific application user id losing access to that shared folder. So we need help in pre advance checking if the access is present for that user id on that shared folder location . So thinking of an application to be developed in C# or vb.net to check for that user access .
Asked
Active
Viewed 3,440 times
1
-
Please keep in mind the difference between "share" permissions and "file system" permissions. You need both to gain access and it gets fairly complex depending on your organization's discipline to sticking to established conventions. – banging Aug 29 '15 at 18:59
1 Answers
2
the way I believe best to check the permission, is try to access the directory (read/write/list) & catch the UnauthorizedAccessException.
If you want to check permissions, following code should satisfy your need. You need to read Access Rules
for the directory.
private bool DirectoryCanListFiles(string folder)
{
bool hasAccess = false;
//Step 1. Get the userName for which, this app domain code has been executing
string executingUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
NTAccount acc = new NTAccount(executingUser);
SecurityIdentifier secId = acc.Translate(typeof(SecurityIdentifier)) as SecurityIdentifier;
DirectorySecurity dirSec = Directory.GetAccessControl(folder);
//Step 2. Get directory permission details for each user/group
AuthorizationRuleCollection authRules = dirSec.GetAccessRules(true, true, typeof(SecurityIdentifier));
foreach (FileSystemAccessRule ar in authRules)
{
if (secId.CompareTo(ar.IdentityReference as SecurityIdentifier) == 0)
{
var fileSystemRights = ar.FileSystemRights;
Console.WriteLine(fileSystemRights);
//Step 3. Check file system rights here, read / write as required
if (fileSystemRights == FileSystemRights.Read ||
fileSystemRights == FileSystemRights.ReadAndExecute ||
fileSystemRights == FileSystemRights.ReadData ||
fileSystemRights == FileSystemRights.ListDirectory)
{
hasAccess = true;
}
}
}
return hasAccess;
}

DeshDeep Singh
- 1,817
- 2
- 23
- 43
-
Thanks Deshdeep. But here I have requirement of checking access for a specific user id. Can you help me in that prospective. – Sai Krishna Jul 28 '15 at 10:05
-
-
Sorry for late reply DeshDeep. Its not working for me. Actually my User id is retaillink\comrlink, but the code is not able to translate. I am still in search on the solution. My main requirement is the access is present on not I need to check and also the folder is a shared production folder. – Sai Krishna Jul 31 '15 at 13:33