Click here to Skip to main content
15,896,606 members
Home / Discussions / C#
   

C#

 
QuestionHow to validate the inputs Pin
Kefaleas Stavros25-Dec-07 10:26
Kefaleas Stavros25-Dec-07 10:26 
AnswerRe: How to validate the inputs Pin
Ed.Poore25-Dec-07 14:48
Ed.Poore25-Dec-07 14:48 
AnswerRe: How to validate the inputs Pin
KaptinKrunch26-Dec-07 8:06
KaptinKrunch26-Dec-07 8:06 
GeneralEffective Security Access Check Pin
Jeffrey Walton25-Dec-07 7:42
Jeffrey Walton25-Dec-07 7:42 
GeneralRe: Effective Security Access Check Pin
Jeffrey Walton25-Dec-07 8:05
Jeffrey Walton25-Dec-07 8:05 
GeneralRe: Effective Security Access Check Pin
martin_hughes26-Dec-07 2:11
martin_hughes26-Dec-07 2:11 
GeneralRe: Effective Security Access Check Pin
Jeffrey Walton28-Dec-07 8:08
Jeffrey Walton28-Dec-07 8:08 
GeneralRe: Effective Security Access Check [modified] Pin
martin_hughes28-Dec-07 9:28
martin_hughes28-Dec-07 9:28 
Hi Jeff,

I've been fiddling a bit - I haven't come up with a total solution (I am a bit hung over), but hopefully it's enough to give you a head start on the syntax etc... On with the code!

static void UserRights()
{
    DirectorySecurity ds = Directory.GetAccessControl(@"C:\test");
    WindowsIdentity user = WindowsIdentity.GetCurrent();

    bool IsInGroupWithWriteAcces = false;
    //This one gets all the groups that a user is in
    IdentityReferenceCollection irc = user.Groups;

    //Loop through all the groups the user is in...
    for (int i = 0; i < irc.Count; i++)
    {

        //Loop through all the access rules in the DirectorySecurity
        foreach (AccessRule ar in ds.GetAccessRules(true, true,
            typeof(System.Security.Principal.SecurityIdentifier)))
        {
            //Cast the AccessRule into a FileSystemAccessRule to make use of it
            FileSystemAccessRule fs = (FileSystemAccessRule)ar;

            //Now test to see if the current group has AccesControlType.Allow
            //and if the FileSystemRights are FileSystemRights.Write
            //I do a logical AND here:
            //      (fs.FileSystemRights & FileSystemRights.Write) == FileSystemRights.Write)
            //because fs.FileSystemRights returns all the
            //FileSystemRights in one go and performing the AND let's us just get
            //the Write that we're interested in.
            if ((fs.AccessControlType == AccessControlType.Allow
                && (fs.FileSystemRights & FileSystemRights.Write) == FileSystemRights.Write) &&
                fs.IdentityReference == irc[i])
            {
                Console.WriteLine("Yay, we're in a group with Write Access!");
                //Since we're in a group with Write access here, we could return true...
                //IsInGroupWithWriteAcces = true;
                //return IsInGroupWithWriteAcces;
            }
            else
            {
                //this group didn't contain write access
                Console.WriteLine("Whoops!");
            }

        }
    }
    return IsInGroupWithWriteAcces;
}


And to check if a User Account has permissions, all you need do is remove the for loop and simply test against the user.User SID.

static void UserRights2()
  {
      DirectorySecurity ds = Directory.GetAccessControl(@"C:\test");
      WindowsIdentity user = WindowsIdentity.GetCurrent();

      bool IsInGroupWithWriteAcces = false;

          //Loop through all the access rules in the DirectorySecurity
          foreach (AccessRule ar in ds.GetAccessRules(true, true,
              typeof(System.Security.Principal.SecurityIdentifier)))
          {
              //Cast the AccessRule into a FileSystemAccessRule to make use of it
              FileSystemAccessRule fs = (FileSystemAccessRule)ar;

              //Now test to see if the current group has AccesControlType.Allow
              //and if the FileSystemRights are FileSystemRights.Write
              //I do a logical AND here:
              //      (fs.FileSystemRights &amp;amp; FileSystemRights.Write) == FileSystemRights.Write)
              //because fs.FileSystemRights returns all the
              //FileSystemRights in one go and performing the AND let's us just get
              //the Write that we're interested in.
              if ((fs.AccessControlType == AccessControlType.Allow
                  && (fs.FileSystemRights & FileSystemRights.Write) == FileSystemRights.Write) &&
                  fs.IdentityReference == user.User)
              {
                  Console.WriteLine("Yay, we're a User with Write Access!");
                  //Since we're in a group with Write access here, we could return true...
                  //IsInGroupWithWriteAcces = true;
                  //return IsInGroupWithWriteAcces;
              }
              else
              {
                  //Were not a User with write access.
                  Console.WriteLine("Whoops!");
              }

          }

      //return IsInGroupWithWriteAcces;
  }



The rest should be just a matter of working out in which order things are applied/take precedence.

Cheers,

Martin


"On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't.

"I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it."
-Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.


modified on Friday, December 28, 2007 4:51:42 PM

GeneralI ate too much... and another nice C# 3.0 feature Pin
martin_hughes25-Dec-07 6:17
martin_hughes25-Dec-07 6:17 
GeneralRe: I ate too much... and another nice C# 3.0 feature Pin
Jeffrey Walton25-Dec-07 7:39
Jeffrey Walton25-Dec-07 7:39 
GeneralRe: I ate too much... and another nice C# 3.0 feature Pin
martin_hughes25-Dec-07 7:50
martin_hughes25-Dec-07 7:50 
GeneralRe: I ate too much... and another nice C# 3.0 feature Pin
Waleed Eissa25-Dec-07 17:09
Waleed Eissa25-Dec-07 17:09 
GeneralRe: I ate too much... and another nice C# 3.0 feature Pin
martin_hughes26-Dec-07 2:28
martin_hughes26-Dec-07 2:28 
GeneralExercise Solution Pin
Kefaleas Stavros24-Dec-07 23:52
Kefaleas Stavros24-Dec-07 23:52 
GeneralRe: Exercise Solution Pin
Luc Pattyn25-Dec-07 0:58
sitebuilderLuc Pattyn25-Dec-07 0:58 
GeneralA simple paint program-C# Pin
Ali Rahimei24-Dec-07 22:48
Ali Rahimei24-Dec-07 22:48 
GeneralRe: A simple paint program-C# Pin
Luc Pattyn25-Dec-07 1:00
sitebuilderLuc Pattyn25-Dec-07 1:00 
GeneralRe: A simple paint program-C# Pin
Paul Conrad25-Dec-07 10:33
professionalPaul Conrad25-Dec-07 10:33 
GeneralA simple paint program-C# Pin
Ali Rahimei24-Dec-07 22:46
Ali Rahimei24-Dec-07 22:46 
GeneralRe: A simple paint program-C# Pin
Giorgi Dalakishvili24-Dec-07 23:03
mentorGiorgi Dalakishvili24-Dec-07 23:03 
QuestionHow i can user User Control in my own Form Pin
wasimsharp24-Dec-07 21:23
wasimsharp24-Dec-07 21:23 
AnswerRe: How i can user User Control in my own Form Pin
Xmen Real 25-Dec-07 2:53
professional Xmen Real 25-Dec-07 2:53 
GeneralRe: How i can user User Control in my own Form Pin
wasimsharp25-Dec-07 19:33
wasimsharp25-Dec-07 19:33 
GeneralRe: How i can user User Control in my own Form Pin
Xmen Real 26-Dec-07 0:40
professional Xmen Real 26-Dec-07 0:40 
GeneralPrint Screen Pin
half-life24-Dec-07 20:19
half-life24-Dec-07 20:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.