| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1136 人关注过本帖
标题:c#中权限是怎么回事
只看楼主 加入收藏
wxzyn123
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-9-15
收藏
 问题点数:0 回复次数:4 
c#中权限是怎么回事
//-----------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
/*=====================================================================
  File:      Permissions.cs
  Summary:   Demonstrates how use code access security.
=====================================================================*/

// Add the classes in the following namespaces to our namespace
using System;
using
using System.Security.Permissions;
using System.Security;

///////////////////////////////////////////////////////////////////////////////
namespace Microsoft.Samples.CAS.Permissions
{
    // This class represents the application itself
    class App
    {
        public static void Main()
        {
            // Try to access resources using the permissions currently available.
            AttemptAccess("Default permissions");
            // Create a permission set that allows read access to the TEMP
            // environment variable and read, write, and append access to SomeFile
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(
                new EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP"));
            ps.AddPermission(
                new FileIOPermission(FileIOPermissionAccess.Read |
                FileIOPermissionAccess.Write | FileIOPermissionAccess.Append,
                Path.GetFullPath("SomeFile")));
            // Use caution in asserting permissions in publicly callable code without
            // any kind of check on the caller.  There is a danger of the assert(断言) being
            // used to exploit(利用) a downstream(下游) caller by stopping its security check,
            // allowing the malicious(恶意) code access to unauthorized resources.
            // Stop security checks at this point in the stack walk
            // for the specified permissions
            ps.Assert();
            // Try to access resources using the permissions we've just asserted.
            AttemptAccess("Assert permissions");
            // Remove this stack frame's Assert
            CodeAccessPermission.RevertAssert();
            // Deny access to the resources we specify
            ps.Deny();
            // Try to access resources using the permissions we've just denied.
            AttemptAccess("Deny permissions");
            // Remove this stack frame's Deny so we're back to default permissions.
            CodeAccessPermission.RevertDeny();
            // Make the permissions indicate the only things that we're allowed to do.
            ps.PermitOnly();
            // Try to access resources using only the permissions we've just permitted.
            AttemptAccess("PermitOnly permissions");
            // Remove this stack frame's PermitOnly so we're back to default permissions.
            CodeAccessPermission.RevertPermitOnly();
            // Remove the FileIOPermissions from the permission set
            ps.RemovePermission(typeof(FileIOPermission));
            // Try to access resources using only the Environment permissions.
            ps.PermitOnly();
            AttemptAccess("PermitOnly without FileIOPermission permissions");
            CodeAccessPermission.RevertPermitOnly();
            // Remove the EnvironmentPermission from the permission set
            ps.RemovePermission(typeof(EnvironmentPermission));
            // Try to access resources using no permissions.
            ps.PermitOnly();
            AttemptAccess("PermitOnly without any permissions");
            CodeAccessPermission.RevertPermitOnly();
            // Show how to use Demand/Assert to improve performance
            CopyFile(".\\Permissions.exe", ".\\Permissions.copy.exe");
            // Delete .exe copy
            File.Delete(".\\Permissions.copy.exe");
        }

        static public void AttemptAccess(String s)
        {
            FileStream fs = null;
            String ev = null;
            String attemptResult = s + " test: ";
            // Try to access a file
            try
            {
                fs = new FileStream("SomeFile", FileMode.OpenOrCreate);
            }
            catch (SecurityException)
            {
                // Handle exception appropriately - for this sample, we will
                // simply ignore the exception
            }
            finally
            {
                if (fs != null)
                {
                    attemptResult += "File opened, ";
                    fs.Close();
                    File.Delete("SomeFile");
                }
                else
                    attemptResult += "File NOT opened, ";
            }
            // Try to read an environment variable
            try
            {
                ev = Environment.GetEnvironmentVariable("TEMP");
            }
            catch (SecurityException)
            {
                // Handle exception appropriately - for this sample, we will
                // simply ignore the exception
            }
            finally
            {
                if (ev != null)
                    attemptResult += "Environment variable read";
                else
                    attemptResult += "Environment variable NOT read";
            }
            Console.WriteLine(attemptResult);
        }

        public static void CopyFile(String srcPath, String dstPath)  
        {
            // Create a file permission set indicating all of this method's intentions.
            FileIOPermission fp = new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath(srcPath));
            fp.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Append, Path.GetFullPath(dstPath));
            // Verify that we can be granted all the permissions we'll need.
            fp.Demand();
            // Assert the desired permissions here.
            fp.Assert();
            // For the remainder of this method, demands for source file read access
            // and demands for destination file write/append access will be granted
            // immediately; walking the remainder of the stack will not be necessary.
            try
            {
                FileInfo srcFile = new FileInfo(srcPath);
                FileInfo dstFile = new FileInfo(dstPath);
                Stream src = srcFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
                Stream dst = dstFile.Open(FileMode.Create, FileAccess.Write, FileShare.None);
                // Note: FileInfo.Length is a Int64, but the Stream.Read and .Write
                // take Int32 counts - hence the casting/throttling necessary below
                if (srcFile.Length > Int32.MaxValue)
                    throw new ArgumentOutOfRangeException("CopyFile requires that the source file be less than 2GB.");
                Byte[] buffer = new Byte[(Int32)srcFile.Length];
                src.Read(buffer, 0, (Int32)srcFile.Length);
                dst.Write(buffer, 0, (Int32)srcFile.Length);
                src.Close();
                dst.Close();
            }
            catch (FileNotFoundException e) { Console.WriteLine("File no found"); }
            Console.ReadLine();
            // We do not need a RevertAssert here because we are going out of scope
        }
    }
}
///////////////////////////////// End of File /////////////////////////////////
搜索更多相关主题的帖子: Microsoft 权限 SDK part AND 
2007-12-09 13:46
wxzyn123
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-9-15
收藏
得分:0 
2007-12-09 14:01
wxzyn123
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-9-15
收藏
得分:0 
只解释下什么是权限也行
2007-12-09 14:02
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
此地水库

msdn == 葵花宝典!!!
QQ:122768959
2007-12-10 10:25
梦心
Rank: 4
来 自:福建平和
等 级:贵宾
威 望:13
帖 子:1910
专家分:0
注 册:2007-5-11
收藏
得分:0 
大一萝代码~

我清高和我骄傲的倔强,在风中大声的唱:我不听,我不听~~做我自己最特别,呼呼~~啦啦~~~
我的博客园地址: [url]http://[/url]
2007-12-10 10:28
快速回复:c#中权限是怎么回事
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019628 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved