Connecting Tech Pros Worldwide Forums | Help | Site Map

Folder level permissions using asp.net

Member
 
Join Date: Aug 2006
Location: USA
Posts: 93
#1: Oct 27 '08
Hi,
I am developing an application in which a user can create folders and set the permission to the specific users. When the folder is screated at the first time no user should have permissions on the folder. But i found that all the users are able to access the folder.
How can I change the folder permissions using asp.net code. Thanks in advance.


Thanks & Regards
Bharath Reddy VasiReddy

Needs Regular Fix
 
Join Date: Mar 2008
Posts: 283
#2: Oct 27 '08

re: Folder level permissions using asp.net


This is some code that I was playing with, it is in vb.net but you might be able to convert it

it is not my tidiest code but it basically adds the current user to the ACL for a folder and sets ' administrators ' as the owner, I had some problems setting the owner before I gave permissions



Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports System.IO
  3. Imports System.Security.Permissions
  4. Imports System.Security.Principal
  5.  
  6.  
  7. Public Class Form1
  8.  
  9.  
  10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  11.         Dim self = System.Security.Principal.WindowsIdentity.GetCurrent()
  12.         Dim Rulz = New System.Security.AccessControl.FileSystemAccessRule(self.Name, System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow) ' create ACL 
  13.         Dim mydir As New DirectoryInfo("c:\test")
  14.         Dim vals As System.Security.AccessControl.FileSystemAccessRule
  15.         Dim x = mydir.GetAccessControl()
  16.         Dim permlists = x.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))
  17.         Dim nt As New System.Security.Principal.NTAccount("", "Administrators")
  18.         Dim gr As System.Security.Principal.IdentityReference = nt
  19.  
  20.         Dim tempown = x.GetOwner(GetType(System.Security.Principal.NTAccount)) ' check current owner
  21.         MsgBox(tempown.Value)
  22.  
  23.         x.AddAccessRule(Rulz)  ' add ACL to X
  24.  
  25.         mydir.SetAccessControl(x) ' set access control oin folder using x
  26.  
  27.         x.SetOwner(nt) ' set owner for folder to administrators
  28.  
  29.         mydir.SetAccessControl(x) ' set access control oin folder using x
  30.  
  31.         Dim num As Integer = 0
  32.  
  33.         For Each vals In permlists ' check current permissions
  34.  
  35.             ListBox1.Items.Add(vals.IdentityReference.Value & " - " & vals.FileSystemRights.ToString)
  36.  
  37.              num += 1
  38.  
  39.         Next
  40.  
  41.  
  42.  
  43.     End Sub
  44. End Class
  45.  
  46.  
Reply