Visual C++ .NET/Security/CodeAccessPermission — различия между версиями

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

Текущая версия на 12:05, 26 мая 2010

Assert Deny Permissions

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace System::Security;
using namespace System::Security::Permissions;
void AssertRead()
{
    CodeAccessPermission ^permission = gcnew FileIOPermission(FileIOPermissionAccess::Read, "C:\\Test");
    permission->Assert();
    StreamReader ^sr = File::OpenText("C:\\TestFile.txt");
    String ^s = sr->ReadLine();
    sr->Close();
    permission->RevertAssert();
    Console::WriteLine("Successful Read");
}
void NoAssertRead()
{
    StreamReader ^sr = File::OpenText("C:\\TestFile.txt");
    String ^s = sr->ReadLine();
    sr->Close();
    Console::WriteLine("Successful Read");
}
void main()
{
    CodeAccessPermission ^permissionRead = gcnew FileIOPermission(FileIOPermissionAccess::Read, "C:\\Test");
    permissionRead->Deny();
    try
    {
        AssertRead();
        NoAssertRead();
    }
    catch(SecurityException^)
    {
        Console::WriteLine("Failed To Read");
    }
    permissionRead->RevertDeny();
    CodeAccessPermission ^permissionWrite = gcnew FileIOPermission(FileIOPermissionAccess::Write, "C:\\Test");

    permissionWrite->PermitOnly();
    try
    {
        AssertRead();
        NoAssertRead();
    }
    catch(SecurityException^)
    {
        Console::WriteLine("Failed To Read");
    }
    permissionWrite->RevertPermitOnly();
}