【问题标题】:What to use instead of SecurityManager.IsGranted?使用什么来代替 SecurityManager.IsGranted?
【发布时间】:2018-11-24 10:26:47
【问题描述】:

我还没有找到我要问的确切问题。

我想查看用户是否有权删除给定的文件或文件夹

我正在使用这个:

Private Function UserHasPermissionsToSource(ByVal source As String) As Boolean    
    Dim writePermission As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, source)
    Return SecurityManager.IsGranted(writePermission)
End Function

但是对于已过时的 isGranted,我不确定用什么替换它。这是我目前所拥有的,有人可以确认这是否正确吗?

Private Function UserHasPermissionsToSource(ByVal source As String) As Boolean
    'Get the permissions of the file/folder
    Dim writePermission As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, source)

    'Create your permission set and add file permissions for write
    Dim permissionSet As PermissionSet = New PermissionSet(PermissionState.None)
    permissionSet.AddPermission(writePermission)

    'checks permissions????
    If permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet) Then
        Return True
    End If

    Return False
End Function

对于相同的结果,似乎需要做更多的工作。

【问题讨论】:

    标签: vb.net permissions user-permissions


    【解决方案1】:

    以上不正确,我找到了解决方案。

    Private Function GetFileAccessControl(ByVal source As String) As String
    
            Dim denied As Boolean = False
            Dim allowed As Boolean = False
    
            'Gets the current User
            Dim wid As WindowsIdentity = WindowsIdentity.GetCurrent()
    
            Try
                Dim arc As AuthorizationRuleCollection = GetAuthorizationRuleCollection(source)
                Dim ars As IList(Of FileSystemAccessRule) = New List(Of FileSystemAccessRule)(arc.OfType(Of FileSystemAccessRule))
                Dim widgs As IList(Of IdentityReference) = New List(Of IdentityReference)
    
                For Each g As SecurityIdentifier In wid.Groups
                    If g.IsAccountSid() Then
                        widgs.Add(g)
                    End If
                Next
    
                'User not inherited rules
                For Each rule As FileSystemAccessRule In (From r In ars Where r.IdentityReference.Equals(wid.User) AndAlso Not r.IsInherited)
                    denied = denied Or DeniesWriteAccess(rule)
                    allowed = allowed Or AllowsWriteAccess(rule)
                Next
    
                For Each rule As FileSystemAccessRule In (From r In ars Where r.IdentityReference.Equals(wid.User) AndAlso r.IsInherited)
                    denied = denied Or DeniesWriteAccess(rule)
                    allowed = allowed Or AllowsWriteAccess(rule)
                Next
    
                For Each rule As FileSystemAccessRule In (From r In ars Where widgs.Contains(r.IdentityReference) AndAlso Not r.IsInherited)
                    denied = denied Or DeniesWriteAccess(rule)
                    allowed = allowed Or AllowsWriteAccess(rule)
                Next
    
                For Each rule As FileSystemAccessRule In (From r In ars Where widgs.Contains(r.IdentityReference) AndAlso r.IsInherited)
                    denied = denied Or DeniesWriteAccess(rule)
                    allowed = allowed Or AllowsWriteAccess(rule)
                Next
            Catch ex As UnauthorizedAccessException
    
            End Try
    
            If Not denied AndAlso allowed Then
                Return True
            End If
    
            Return False
        End Function
    
        Private Function GetAuthorizationRuleCollection(ByVal source As String) As AuthorizationRuleCollection
    
            'Gets the current User
            Dim wid As WindowsIdentity = WindowsIdentity.GetCurrent()
            Dim arc As AuthorizationRuleCollection
            arc = Nothing
    
            If (Directory.Exists(source)) Then
                Dim di As DirectoryInfo = New DirectoryInfo(source)
                Dim acl As DirectorySecurity = di.GetAccessControl()
                arc = acl.GetAccessRules(True, True, GetType(SecurityIdentifier))
            ElseIf File.Exists(source) Then
                Dim fi As FileInfo = New FileInfo(source)
                Dim acl As FileSecurity = fi.GetAccessControl()
                arc = acl.GetAccessRules(True, True, GetType(SecurityIdentifier))
            End If
    
            Return arc
        End Function
    
        Private Function AllowsWriteAccess(rule As FileSystemAccessRule) As Boolean
            If rule.AccessControlType = AccessControlType.Allow AndAlso
                    (rule.FileSystemRights.HasFlag(FileSystemRights.Write) OrElse
                    rule.FileSystemRights.HasFlag(FileSystemRights.WriteData) OrElse
                    rule.FileSystemRights.HasFlag(FileSystemRights.CreateDirectories) OrElse
                    rule.FileSystemRights.HasFlag(FileSystemRights.CreateFiles)) Then
                Return True
            End If
            Return False
        End Function
    
        Private Function DeniesWriteAccess(rule As FileSystemAccessRule) As Boolean
            If rule.AccessControlType = AccessControlType.Deny AndAlso
                    (rule.FileSystemRights.HasFlag(FileSystemRights.Write) OrElse
                    rule.FileSystemRights.HasFlag(FileSystemRights.WriteData) OrElse
                    rule.FileSystemRights.HasFlag(FileSystemRights.CreateDirectories) OrElse
                    rule.FileSystemRights.HasFlag(FileSystemRights.CreateFiles)) Then
                Return True
            End If
            Return False
        End Function
    

    【讨论】:

    • 第一个定义的函数GetFileAccessControl应该是As Boolean,而不是As String,因为它返回的是一个布尔值。
    • 我还建议将第一个函数命名为HasWriteAccessForFile,因为它是专门针对写访问的检查,而不仅仅是针对任何权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多