VB.Net/Development/Security — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Check File IO Permission

Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.IO

public class MainClass
   Shared Sub Main()
        Dim oFp As FileIOPermission = New _
        FileIOPermission(FileIOPermissionAccess.Write, "d:\test.txt")
        oFp.Assert()
        Try
            Dim objWriter As New IO.StreamWriter(File.Open("d:\test.txt", IO.FileMode.Open))
            objWriter.WriteLine("Hi there!")
            objWriter.Flush()
            objWriter.Close()
            objWriter = Nothing
        Catch objA As System.Exception
            Console.WriteLine(objA.Message)
        End Try
   End Sub
End Class


Environment Permissions Demo

Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.IO

public class MainClass
   Shared Sub Main()
        Dim oEp As EnvironmentPermission = New EnvironmentPermission(EnvironmentPermissionAccess.Read, "Temp")
        Dim sEv As String
        oEp.Assert()
        Try
            sEv = Environment.GetEnvironmentVariable("Temp")
            Console.WriteLine("Assert was a success")
        Catch objA As System.Exception
            Console.WriteLine("Assert failed")
        End Try
        System.Security.CodeAccessPermission.RevertAssert()
        oEp.Deny()
        Try
            sEv = Environment.GetEnvironmentVariable("Temp")
            Console.WriteLine("Deny was a success")
        Catch objA As System.Exception
            Console.WriteLine("Deny failed")
        End Try
        Console.WriteLine(oEp.ToString)
   End Sub
End Class


Registry Permissions Demo

Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.IO
Imports System.Diagnostics
public class MainClass
   Shared Sub Main()
        Dim oRp As New RegistryPermission(Security.Permissions.PermissionState.Unrestricted)
        oRp.Deny()
        Dim objLog As New EventLog
        Dim objLogEntryType As EventLogEntryType
        Try
            Throw (New EntryPointNotFoundException)
        Catch objA As System.EntryPointNotFoundException
            Try
                If Not System.Diagnostics.EventLog.SourceExists("Example") Then
                    System.Diagnostics.EventLog.CreateEventSource("Example", "System")
                End If
                objLog.Source = "Example"
                objLog.Log = "System"
                objLogEntryType = EventLogEntryType.Information
                objLog.WriteEntry("Error: " & objA.Message, objLogEntryType)
            Catch objB As System.Exception
                Console.WriteLine(objB.Message)
            End Try
        End Try
   End Sub
End Class