SecurityAction Výčet
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Upozornění
Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}
Upozornění
CAS support is not available with Silverlight applications.
Určuje akce zabezpečení, které lze provést pomocí deklarativního zabezpečení.
public enum class SecurityAction
[System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public enum SecurityAction
public enum SecurityAction
[System.Serializable]
public enum SecurityAction
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum SecurityAction
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Obsolete("CAS support is not available with Silverlight applications.")]
public enum SecurityAction
[<System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type SecurityAction =
type SecurityAction =
[<System.Serializable>]
type SecurityAction =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SecurityAction =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Obsolete("CAS support is not available with Silverlight applications.")>]
type SecurityAction =
Public Enum SecurityAction
- Dědičnost
- Atributy
Pole
Assert | 3 | Volající kód má přístup k prostředku identifikovanému aktuálním objektem oprávnění, i když volajícím vyšší v zásobníku nebylo uděleno oprávnění pro přístup k prostředku (viz Použití metody Assert). |
Demand | 2 | Všichni volající vyšší v zásobníku volání musí mít udělená oprávnění určená aktuálním objektem oprávnění. |
Deny | 4 | Možnost přístupu k prostředku určenému aktuálním objektem oprávnění je volajícím odepřena, i když mu bylo uděleno oprávnění k přístupu (viz Použití metody Odepřít). |
InheritanceDemand | 7 | Odvozená třída, která dědí třídu nebo přepisuje metodu, musí být udělena zadaná oprávnění. |
LinkDemand | 6 | Volající musí mít udělené zadané oprávnění. Nepoužívejte v rozhraní .NET Framework 4. Pro plnou důvěryhodnost použijte SecurityCriticalAttribute místo toho; pro částečnou důvěryhodnost použijte Demand. |
PermitOnly | 5 | Přístup k prostředkům určeným tímto objektem oprávnění lze získat, i když byl kód udělen oprávnění pro přístup k jiným prostředkům. |
RequestMinimum | 8 | Požadavek na minimální oprávnění vyžadovaná ke spuštění kódu. Tuto akci lze použít pouze v rámci rozsahu sestavení. |
RequestOptional | 9 | Požadavek na další oprávnění, která jsou nepovinná (nevyžaduje se ke spuštění). Tento požadavek implicitně odmítne všechna ostatní oprávnění, která nejsou výslovně požadována. Tuto akci lze použít pouze v rámci rozsahu sestavení. |
RequestRefuse | 10 | Požadavek, aby se volajícímu kódu neudělila oprávnění, která by mohla být zneužita. Tuto akci lze použít pouze v rámci rozsahu sestavení. |
Příklady
Tento příklad ukazuje, jak upozornit CLR, že kód volaných metod má pouze IsolatedStoragePermission, a také ukazuje, jak zapisovat a číst z izolovaného úložiště.
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::IO::IsolatedStorage;
using namespace System::IO;
static void WriteIsolatedStorage()
{
try
{
// Attempt to create a storage file that is isolated by
// user and assembly. IsolatedStorageFilePermission
// granted to the attribute at the top of this file
// allows CLR to load this assembly and execution of this
// statement.
Stream^ fileCreateStream = gcnew
IsolatedStorageFileStream(
"AssemblyData",
FileMode::Create,
IsolatedStorageFile::GetUserStoreForAssembly());
StreamWriter^ streamWriter = gcnew StreamWriter(
fileCreateStream);
try
{
// Write some data out to the isolated file.
streamWriter->Write("This is some test data.");
streamWriter->Close();
}
finally
{
delete fileCreateStream;
delete streamWriter;
}
}
catch (IOException^ ex)
{
Console::WriteLine(ex->Message);
}
try
{
Stream^ fileOpenStream =
gcnew IsolatedStorageFileStream(
"AssemblyData",
FileMode::Open,
IsolatedStorageFile::GetUserStoreForAssembly());
// Attempt to open the file that was previously created.
StreamReader^ streamReader = gcnew StreamReader(
fileOpenStream);
try
{
// Read the data from the file and display it.
Console::WriteLine(streamReader->ReadLine());
streamReader->Close();
}
finally
{
delete fileOpenStream;
delete streamReader;
}
}
catch (FileNotFoundException^ ex)
{
Console::WriteLine(ex->Message);
}
catch (IOException^ ex)
{
Console::WriteLine(ex->Message);
}
}
// Notify the CLR to only grant IsolatedStorageFilePermission to called methods.
// This restricts the called methods to working only with storage files that are isolated
// by user and assembly.
[IsolatedStorageFilePermission(SecurityAction::PermitOnly, UsageAllowed = IsolatedStorageContainment::AssemblyIsolationByUser)]
int main()
{
WriteIsolatedStorage();
}
// This code produces the following output.
//
// This is some test data.
using System;
using System.Security.Permissions;
using System.IO.IsolatedStorage;
using System.IO;
// Notify the CLR to only grant IsolatedStorageFilePermission to called methods.
// This restricts the called methods to working only with storage files that are isolated
// by user and assembly.
[IsolatedStorageFilePermission(SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
public sealed class App
{
static void Main()
{
WriteIsolatedStorage();
}
private static void WriteIsolatedStorage()
{
// Attempt to create a storage file that is isolated by user and assembly.
// IsolatedStorageFilePermission granted to the attribute at the top of this file
// allows CLR to load this assembly and execution of this statement.
using (Stream s = new IsolatedStorageFileStream("AssemblyData", FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly()))
{
// Write some data out to the isolated file.
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write("This is some test data.");
}
}
// Attempt to open the file that was previously created.
using (Stream s = new IsolatedStorageFileStream("AssemblyData", FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly()))
{
// Read the data from the file and display it.
using (StreamReader sr = new StreamReader(s))
{
Console.WriteLine(sr.ReadLine());
}
}
}
}
// This code produces the following output.
//
// Some test data.
Option Strict On
Imports System.Security.Permissions
Imports System.IO.IsolatedStorage
Imports System.IO
' Notify the CLR to only grant IsolatedStorageFilePermission to called methods.
' This restricts the called methods to working only with storage files that are isolated
' by user and assembly.
<IsolatedStorageFilePermission(SecurityAction.PermitOnly, UsageAllowed:=IsolatedStorageContainment.AssemblyIsolationByUser)> _
Public NotInheritable Class App
Shared Sub Main()
WriteIsolatedStorage()
End Sub
Shared Sub WriteIsolatedStorage()
' Attempt to create a storage file that is isolated by user and assembly.
' IsolatedStorageFilePermission granted to the attribute at the top of this file
' allows CLR to load this assembly and execution of this statement.
Dim s As New IsolatedStorageFileStream("AssemblyData", FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly())
Try
' Write some data out to the isolated file.
Dim sw As New StreamWriter(s)
Try
sw.Write("This is some test data.")
Finally
sw.Dispose()
End Try
Finally
s.Dispose()
End Try
' Attempt to open the file that was previously created.
Dim t As New IsolatedStorageFileStream("AssemblyData", FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly())
Try
' Read the data from the file and display it.
Dim sr As New StreamReader(t)
Try
Console.WriteLine(sr.ReadLine())
Finally
sr.Dispose()
End Try
Finally
t.Dispose()
End Try
End Sub
End Class
' This code produces the following output.
'
' Some test data.
Poznámky
Upozornění
Zabezpečení přístupu k kódu (CAS) je zastaralé ve všech verzích rozhraní .NET Framework a .NET. Nedávné verze rozhraní .NET nedotknou poznámek CAS a v případě použití rozhraní API souvisejících s CAS se nevygenerují chyby. Vývojáři by měli hledat alternativní způsoby provádění úloh zabezpečení.
Následující tabulka popisuje čas, kdy se provádí každá akce zabezpečení, a cíle, které podporuje.
Důležité
V rozhraní .NET Framework 4 byla podpora modulu runtime odebrána pro vynucování žádostí o oprávnění Deny, RequestMinimum, RequestOptional a RequestRefuse. Tyto požadavky by se neměly používat v kódu založeném na rozhraní .NET Framework 4 nebo novějším. Další informace o těchto a dalších změnách naleznete v tématu Změny zabezpečení.
V rozhraní .NET Framework 4 byste neměli používat LinkDemand
. Místo toho použijte SecurityCriticalAttribute omezení využití na plně důvěryhodné aplikace nebo použijte Demand
k omezení částečně důvěryhodných volajících.
Deklarace akce zabezpečení | Čas akce | Podporované cíle |
---|---|---|
LinkDemand (nepoužívejte v rozhraní .NET Framework 4) |
Kompilace za běhu | Třída, metoda |
InheritanceDemand |
Doba načítání | Třída, metoda |
Demand |
Za běhu | Třída, metoda |
Assert |
Za běhu | Třída, metoda |
Deny (zastaralé v rozhraní .NET Framework 4) |
Za běhu | Třída, metoda |
PermitOnly |
Za běhu | Třída, metoda |
RequestMinimum (zastaralé v rozhraní .NET Framework 4) |
Doba udělení | Sestavení |
RequestOptional (zastaralé v rozhraní .NET Framework 4) |
Doba udělení | Sestavení |
RequestRefuse (zastaralé v rozhraní .NET Framework 4) |
Doba udělení | Sestavení |
Další informace o cílech atributů naleznete v tématu Attribute.