IsolatedStorageFilePermissionAttribute 类

允许对使用声明安全性应用到代码中的 IsolatedStorageFilePermission 进行安全操作。无法继承此类。

**命名空间:**System.Security.Permissions
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Constructor Or AttributeTargets.Method, AllowMultiple:=True, Inherited:=False)> _
Public NotInheritable Class IsolatedStorageFilePermissionAttribute
    Inherits IsolatedStoragePermissionAttribute
用法
Dim instance As IsolatedStorageFilePermissionAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false)] 
public sealed class IsolatedStorageFilePermissionAttribute : IsolatedStoragePermissionAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Constructor|AttributeTargets::Method, AllowMultiple=true, Inherited=false)] 
public ref class IsolatedStorageFilePermissionAttribute sealed : public IsolatedStoragePermissionAttribute
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false) */ 
public final class IsolatedStorageFilePermissionAttribute extends IsolatedStoragePermissionAttribute
SerializableAttribute 
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false) 
public final class IsolatedStorageFilePermissionAttribute extends IsolatedStoragePermissionAttribute

备注

允许的声明范围取决于使用的 SecurityAction

安全属性所声明的安全信息存储在属性目标的元数据中,系统在运行时会访问这些信息。安全属性仅用于声明性安全。对于强制性安全,应使用相应的权限类。

示例

下面的声明性属性示例显示请求 IsolatedStorageFilePermission、规定至少要具有此权限才能运行代码,以及请求 5 MB 的最大用户配额的正确方法。

<Assembly: IsolatedStorageFilePermissionAttribute( _
SecurityAction.RequestMinimum, UserQuota := 5242880)>
'In Visual Basic, you must specify that you are using the assembly scope when making a request.
[assembly:IsolatedStorageFilePermissionAttribute(
SecurityAction.RequestMinimum, UserQuota=5242880)]
//In C#, you must specify that you are using the assembly scope when making a request.
[assembly:IsolatedStorageFilePermissionAttribute(
SecurityAction::RequestMinimum,UserQuota=5242880)];
//In C++, you must specify that you are using the assembly scope when making a request.
/** @assembly IsolatedStorageFilePermissionAttribute(SecurityAction.
    RequestMinimum, UserQuota = 5242880)
 */
// In VJ#, you must specify that you are using the assembly scope when making
// a request.

下面的示例显示如何要求调用代码在链接时拥有无限制的 IsolatedStorageFilePermission。通常在托管库 (DLL) 中发出请求,以帮助保护方法或类不受潜在的有害代码的影响。

<IsolatedStorageFilePermissionAttribute( _
 SecurityAction.Demand, Unrestricted := True)> _
 Public Class SampleClass
[IsolatedStorageFilePermissionAttribute(SecurityAction.Demand,
Unrestricted=true)]
[IsolatedStorageFilePermissionAttribute(SecurityAction::Demand,
Unrestricted=true)]
/** @attribute IsolatedStorageFilePermissionAttribute(SecurityAction.Demand, 
    Unrestricted = true)
 */

此示例演示如何通知 CLR 此程序集中的代码需要 IsolatedStoragePermission,还演示如何对独立存储进行读写。

using System;
using System.Security.Permissions;
using System.IO.IsolatedStorage;
using System.IO;

// Notify the CLR to grant this assembly the IsolatedStorageFilePermission. 
// This allows the assembly to work with storage files that are isolated 
// by user and assembly.
[assembly: IsolatedStorageFilePermission(SecurityAction.RequestMinimum, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]

public sealed class App
{
    static void Main()
    {
        // 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.
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::IO::IsolatedStorage;
using namespace System::IO;

// Notify the CLR to grant this assembly the IsolatedStorage-
// FilePermission. This allows the assembly to work with storage
// files that are isolated by user and assembly.
[assembly: IsolatedStorageFilePermission(
    SecurityAction::RequestMinimum, UsageAllowed =
    IsolatedStorageContainment::AssemblyIsolationByUser)];
int main()
{
    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);
    }
}

// This code produces the following output.
//
//  This is some test data.

继承层次结构

System.Object
   System.Attribute
     System.Security.Permissions.SecurityAttribute
       System.Security.Permissions.CodeAccessSecurityAttribute
         System.Security.Permissions.IsolatedStoragePermissionAttribute
          System.Security.Permissions.IsolatedStorageFilePermissionAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

IsolatedStorageFilePermissionAttribute 成员
System.Security.Permissions 命名空间
IsolatedStorageFilePermission 类
IsolatedStoragePermission
IsolatedStoragePermissionAttribute
IsolatedStorageContainment 枚举

其他资源

利用属性扩展元数据