How check is app has file write security permissions?

Discussion in 'ASP .Net' started by Ed Sutton, Sep 28, 2006.

  1. Ed Sutton

    Ed Sutton Guest

    Is there an easy way to check using .NET 2.0?

    Thanks in advance,

    -Ed
    Ed Sutton, Sep 28, 2006
    #1
    1. Advertising

  2. Laurent Bugnion, Sep 28, 2006
    #2
    1. Advertising

  3. Ed Sutton

    Ed Sutton Guest

    |I answer to my own question, I wrote the following which does what I
    needed.


    /// <summary>
    /// Returns true if specified identity name has the file system
    rights for the specified file
    /// </summary>
    private static bool IdentityHasAccces(string identityName,
    FileInfo fileInfo,
    FileSystemRights
    fileSystemRights)
    {
    identityName = identityName.ToUpper();
    AuthorizationRuleCollection authorizationRuleCollection =
    fileInfo.GetAccessControl().GetAccessRules(true, true,
    typeof(NTAccount));
    foreach (FileSystemAccessRule fileSystemAccessRule in
    authorizationRuleCollection)
    {
    if (identityName ==
    fileSystemAccessRule.IdentityReference.Value.ToUpper())
    {
    return AccessControlType.Allow ==
    fileSystemAccessRule.AccessControlType &&
    fileSystemRights ==
    (fileSystemAccessRule.FileSystemRights & fileSystemRights);
    }
    }
    return false;
    }


    if (false ==
    IdentityHasAccces(System.Security.Principal.WindowsIdentity.GetCurrent().Name,

    dbFileInfo,
    FileSystemRights.Read | FileSystemRights.Write))
    {
    return;
    }
    |
    Ed Sutton, Sep 29, 2006
    #3
  4. Ed Sutton

    Ed Sutton Guest

    Hi Laurent,

    Thank you for your reply.

    > Try to write, and catch the Exception.


    Good idea, that would have been much easier. I ended up writing a lot
    of code using the following method:


    /// <summary>
    /// Returns true if specified identity name has the file system
    /// rights for the specified file
    /// </summary>
    private static bool IdentityHasAccces(string identityName,
    FileInfo fileInfo,
    FileSystemRights
    fileSystemRights)
    {
    identityName = identityName.ToUpper();
    AuthorizationRuleCollection authorizationRuleCollection =
    fileInfo.GetAccessControl().GetAccessRules(true,
    true,typeof(NTAccount));

    foreach (FileSystemAccessRule fileSystemAccessRule in
    authorizationRuleCollection)
    {
    if (identityName ==
    fileSystemAccessRule.IdentityReference.Value.ToUpper())
    {
    return AccessControlType.Allow ==
    fileSystemAccessRule.AccessControlType && fileSystemRights
    ==(fileSystemAccessRule.FileSystemRights & fileSystemRights);
    }
    }
    return false;
    }

    Example Usage:
    --------------

    if (false ==
    IdentityHasAccces(System.Security.Principal.WindowsIdentity.GetCurrent().Name,
    dbFileInfo,
    FileSystemRights.Read | FileSystemRights.Write))
    {
    return;
    }
    Ed Sutton, Oct 3, 2006
    #4
  5. Hi,

    Ed Sutton wrote:
    > Hi Laurent,
    >
    > Thank you for your reply.
    >
    >> Try to write, and catch the Exception.

    >
    >
    > Good idea, that would have been much easier. I ended up writing a lot
    > of code using the following method:


    When did you get my message on your newsreader? I replied on the 28th of
    Sept, 10 minutes after you posted. One day later you posted again in the
    same thread, giving no indication that you had read my post. Maybe you
    should consider using a different provider if my post showed late. It
    would have saved you from writing the long code :)

    Greetings,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Oct 3, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    0
    Views:
    317
    Roger
    Oct 12, 2005
  2. puzzlecracker
    Replies:
    4
    Views:
    270
    Jim Langston
    Oct 31, 2005
  3. Leyla
    Replies:
    2
    Views:
    658
    Leyla
    Aug 17, 2006
  4. mark | r

    write permissions and security

    mark | r, Sep 6, 2004, in forum: ASP General
    Replies:
    2
    Views:
    115
    Roland Hall
    Sep 6, 2004
  5. Peter Loftus
    Replies:
    3
    Views:
    117
    Robert Klemme
    Nov 26, 2007
Loading...

Share This Page