ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Security(安全)
1.A,Security 示例(Sample) 返回顶部

“安全”示例

本示例演示如何通过权限类和权限特性来修改安全权限。 有关其他信息,请参见安全(C# 编程指南)。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。 对于因将此代码示例用于其他用途而发生的偶然或必然损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“安全”示例

<![if !supportLists]>1.      <![endif]>在“解决方案资源管理器”中,右击“Security”项目,然后单击“设为启动项目”。

<![if !supportLists]>2.      <![endif]>在“调试”菜单上,单击“开始执行(不调试)”。

从命令行生成并运行“安全”示例

<![if !supportLists]>1.      <![endif]>使用“更改目录”命令转到“Security”目录。

<![if !supportLists]>2.      <![endif]>键入以下命令:

csc Security.cs
Security

若要对本产品的“帮助”或其他功能提出建议或报告 Bug,请访问反馈站点

1.B,示例代码(Sample Code)返回顶部

1.B.1, Security.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Permissions;
using System.Runtime.InteropServices; public class MainClass
{
public static void Main()
{
//创建文件 IO 读取权限
FileIOPermission FileIOReadPermission = new FileIOPermission(PermissionState.None);
FileIOReadPermission.AllLocalFiles = FileIOPermissionAccess.Read; //创建基本权限集
PermissionSet BasePermissionSet = new PermissionSet(PermissionState.None); // PermissionState.Unrestricted 用于完全信任
BasePermissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); PermissionSet grantset = BasePermissionSet.Copy();
grantset.AddPermission(FileIOReadPermission); //编写示例源文件以读取
System.IO.File.WriteAllText("TEST.TXT", "File Content"); //-------- 完全信任地调用方法 --------
try
{
Console.WriteLine("App Domain Name: " + AppDomain.CurrentDomain.FriendlyName);
ReadFileMethod();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} //-------- 创建具有文件 IO 读取权限的 AppDomain --------
AppDomain sandbox = AppDomain.CreateDomain("Sandboxed AppDomain With FileIO.Read permission", AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.SetupInformation, grantset, null);
try
{
Console.WriteLine("App Domain Name: " + AppDomain.CurrentDomain.FriendlyName);
sandbox.DoCallBack(new CrossAppDomainDelegate(ReadFileMethod));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} //-------- 创建没有文件 IO 读取权限的 AppDomain --------
//应当引发安全异常
PermissionSet grantset2 = BasePermissionSet.Copy();
AppDomain sandbox2 = AppDomain.CreateDomain("Sandboxed AppDomain Without FileIO.Read permission", AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.SetupInformation, grantset2, null);
try
{
Console.WriteLine("App Domain Name: " + AppDomain.CurrentDomain.FriendlyName);
sandbox2.DoCallBack(new CrossAppDomainDelegate(ReadFileMethod));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} Console.WriteLine("");
Console.WriteLine("Press any key to end.");
Console.ReadKey();
} static public void ReadFileMethod()
{
string S = System.IO.File.ReadAllText("TEST.TXT");
Console.WriteLine("File Content: " + S);
Console.WriteLine("");
} }

1.B.2,

1.B.EXE,

App Domain Name: ConsoleApplication1.exe
File Content: File Content App Domain Name: ConsoleApplication1.exe
File Content: File Content App Domain Name: ConsoleApplication1.exe
请求“System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, C
ulture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败。 Press any key to end.

1.B,

1.C,下载地址(Free Download)返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ylbtech-LanguageSamples-Security(安全)的更多相关文章

  1. Security Policy:行级安全(Row-Level Security)

    行级安全RLS(Row-Level Security)是在数据行级别上控制用户的访问,控制用户只能访问数据库表的特定数据行.断言是逻辑表达式,在SQL Server 2016中,RLS是基于安全断言( ...

  2. Content Security Policy 入门教程

    阮一峰文章:Content Security Policy 入门教程

  3. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  4. WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题

    摘要 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication ...

  5. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  6. SimpleSSO:使用Microsoft.Owin.Security.OAuth搭建OAuth2.0授权服务端

    目录 前言 OAuth2.0简介 授权模式 (SimpleSSO示例) 使用Microsoft.Owin.Security.SimpleSSO模拟OpenID认证 通过authorization co ...

  7. spring mvc 和spring security配置 spring-servlet.xml和spring-security.xml设置

    spring-servlet.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  8. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  9. 无法解决“Microsoft.SharePoint.Security, Version=15.0.0.0,”与“Microsoft.SharePoint.Security, Version=14.0.0.0”之间的冲突

    VisualStudio 2013创建控制台项目,.NetFramework选为4.5.生成目标平台:x64.然后添加对Microsoft.SharePoint.dll的引用. 生成项目时," ...

  10. iOS App 不支持http协议 App Transport Security has blocked a cleartext HTTP (http://)

    目前iOS已经不支持http协议了,不过可以通过info.plist设置允许 App Transport Security has blocked a cleartext HTTP (http://) ...

随机推荐

  1. linux命令(26):ls命令

    例一:列出/home/peidachang文件夹下的所有文件和目录的详细资料 ls -l -R /home/test 例二:列出当前目录中所有以“t”开头的目录的详细内容,可以使用如下命令: ls - ...

  2. eclipse out of memory

    eclipse 安装目录 修改 eclipse.ini 在eclipse根目录下打开eclipse.ini,默认内容为(这里设置的是运行当前开发工具的JVM内存分配): -vmargs-Xms40m- ...

  3. 《深入浅出MyBatis技术原理与实战》——3. 配置

    要注意的是上面那些层次是不能够颠倒顺序的,否则MyBatis在解析文件的时候就会出现异常. 3.1 properties元素 properties是一个属性配置元素,让我们能在配置文件的上下文中使用它 ...

  4. Sort List——经典(链表中的归并排序)

    Sort a linked list in O(n log n) time using constant space complexity.    对一个链表进行排序,且时间复杂度要求为 O(n lo ...

  5. yum -y install 和yum install 的区别

    yum -y install 包名(支持*) :自动选择y,全自动 yum install 包名(支持*) :手动选择y or n yum remove 包名(不支持*) rpm -ivh 包名(支持 ...

  6. HDU 6464.免费送气球-动态开点-权值线段树(序列中第first小至第second小的数值之和)(感觉就是只有一个状态的主席树) (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  7. phpstorm中Xdebug的使用

    目 录 1.Xdebug简介 2.Xdebug的安装.操作   2.1环境搭建 2.2配置php.ini 2.3配置PhpStorm 2.4配置PHP Debug 2.5进行调试 1.Xdebug简介 ...

  8. HDU 1561 The more, The Better【树形DP/有依赖的分组背包】

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

  9. Unity游戏开发之C#快速入门

    C#是微软团队在开发.NET框架时开发的,它的构想接近于C.C++,也和JAVA十分相似,有许多强大的编程功能. 个人感受是C#吸收了众多编程语言的优点,从中可以看到C.C++.Java.Javasc ...

  10. 洛谷——P1109 学生分组

    P1109 学生分组 题目描述 有N组学生,给出初始时每组中的学生个数,再给出每组学生人数的上界R和下界L(L<=R),每次你可以在某组中选出一个学生把他安排到另外一组中,问最少要多少次才可以使 ...