0

//I need to get permissions of Network shared folders and display in Grid.. When retrieving local files shares it shows the accounts name correctly but when i try to retrieve shared folder permission it displays SID for account name instead of Showing username.. so i use the above code to convert SID value to username and displays in the grid. But the problem is while retrieving directly username or group name for some objects it throws error on it.. so that i want to add a if condition before the coding begins i.e., if it receives User object name in IDentityReference directly it shows to grid value else if it receives SID value in IDentityReference it comes into the loop and convert it into username..//

string sid = ace.IdentityReference.Value;

SecurityIdentifier s = new SecurityIdentifier(sid);

string username = s.Translate(typeof(NTAccount)).Value;

dtrow["Account"] = username;

Sandy
  • 1
  • 3

1 Answers1

0

Try this

        string sid = ace.IdentityReference.Value;
        string username = sid;
        try
        {
            SecurityIdentifier s = new SecurityIdentifier(sid);
            username = s.Translate(typeof(NTAccount)).Value;
        }
        catch
        {
            //ignore
        }
        dtrow["Account"] = username;

If-Else

        string sid = ace.IdentityReference.Value;
        string username = "";
        if (sid.StartsWith("s-", StringComparison.CurrentCultureIgnoreCase))
        {
            SecurityIdentifier s = new SecurityIdentifier(sid);
            username = s.Translate(typeof(NTAccount)).Value;
        }
        else
        {
            username = sid; 

        }
        dtrow["Account"] = username;
Sekhar
  • 5,614
  • 9
  • 38
  • 44
  • I got error, Some or all identity reference could not be translated.. For this issue only we need to add If condition to check whether IdentityReferece value recieves Direct name or SID value.. – Sandy Jun 16 '21 at 06:58
  • the try-catch should work, anyway, have updated the answer with if-else – Sekhar Jun 16 '21 at 07:07
  • Bro I'm getting the same error again...i also checked using Breakpoint,,while coming to convert SID value it throws – Sandy Jun 16 '21 at 07:53
  • Please use the first method (try-catch), and if possible share the breakpoint screenshot. – Sekhar Jun 16 '21 at 07:57