????? C# .NET, ????? C# 2005 ?? ??? ?? ????????? ?? ????? ?? ???? ??? ??? ?? ??? ??????? ??? ?? ?? ?? ???? ??????? ??? ???? ?? ??? AllowPartiallyTrustedCallers ??????? ?? ????? ???? ????

???? ?????? ???? ??????
???? ID: 839300 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
???????????? ???? ??? ??????? ?? ?? ???? ??????? ???????? ?? ?? ???? ??? ??? ???? ?? ??? ?? ???? ??? ???????? ?? ??????? ???????? ?? ??? ???? ?? ??? ???? ?????? ??? ?? ???? ????? ?????? ?? ?? ???? ?? ??? ?? ?????????? ?? ????? ?? ???? ???.. ???? ???? ?? ?? ?? ???????? ????, ?? ??????? ???? ??? ?? ?? ???? ????? ?????? ??? ?? ???? ?????? ??????????? ???? ?? ?????? ??????? ?? ????????? ????.. ??? ?? ?? ????? ?????? ???, ?? ???? ?????? ?? ??????? ?? ??? ?? ?? ???????? ??? ?????? ??? ???? ????? ?? ???..
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ???? ??? ??? ?? ??? Windows ?????????? ???????? ?? ??? ?? ???? ???? ?? ???????? ?????? ?? ????? ???? ??? ?? ?? ????? ??? ?? ?? ???? ??????? ?? ??? ??? ????? ?? ?? Windows ?????????? ???????? ?? ??? ???? ?? ??? ?? ?? ??? Windows ?????????? ???????? ?? ??????? ??????? ???? ????AllowPartiallyTrusted??????? ???????? ?? ?? ??? ???? ?? ??? ?? ????? ????Assert???????? ?????? ?? ??????? ?? ??? Windows ?????????? ???????? ?? ?????? ???? ?? ??? ?????? ???

?????

?? ??? ?? ??? ???? ????? ?? ?? ???? Windows ?? ??? ???? ?? ??? ???? ??? ??? ?? ?????????? ????????? ?? ???? ????? ???? ?? ?? Windows ?????????? ???????? ?? ?? ????? ??? ?? ?? ???? ??????? ?? ??? ??? ????? ??? ?? ??? ??? ????? ?? ????? ???? ?? ??? ???? ?? ????? ???? ??AllowPartiallyTrustedCallers??????? ?? ??????? ?? ??? ??? ?? ??? ??????? ?? ?????? ?? ????? ??? ?? ?????? ?? ????? ????? ???? ??? ????? ?? ????? ???? ?? ??? ???? ?? ???? ??Assert?????? ???

?? ????? ??? ??? ?? ????? ???? ?????

????? ???? ?? ????? ??? ?? ?? ?? ????? ??? ?? ?? ?????????? ???????? ?? ??? ??? ??????? ??? ???? ?? ???? ?? ??? ????? ???? ???? ??? ??? ??? ????? ???? ???? ?? ?? ?? ???? ????? ??? ?? ????????? ??? ?? ??????? ?? ????? ???? ?? ?????? ???? ?? ?? ??? ???? ????? ????
  1. ???? Visual Studio ????? ????????? ??????

    ??? Microsoft Visual Studio .NET 2002, ????? ????????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio .NET?? ????? ????,????? Studio .NET ??????? ????-????? ????, ?? ???? ???????? Studio .NET ????? ?????????. Visual Studio .NET ????? ????????? ????? ????? ???

    Microsoft Visual Studio .NET 2003 ???, ????? ????????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio .NET 2003?? ????? ????,????? Studio .NET ??????? ????-????? ????, ?? ???? ???????? Studio .NET 2003 ????? ?????????. Visual Studio .NET 2003 ????? ????????? ????? ????? ???? ???

    Visual Studio 2005, ??? ????? ????????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio 2005?? ????? ????,????? Studio 2005 ??????? ????-????? ????, ?? ???? ???????? Studio 2005 ????? ?????????. Visual Studio 2005 ????? ????????? ????? ????? ???? ???

    Visual Studio 2008, ??? ????? ????????????? ????? ????,????????????? ????? ????,Microsoft Visual Studio 2008?? ????? ????,????? Studio 2008 ??????? ????-????? ????, ?? ???? ???????? Studio 2008 ????? ?????????. Visual Studio 2008Command ????? ????? ????? ???? ???
  2. ??????? ????????? ?? ????? ???? ???? ?? ???? ??? ENTER ?????::
    SN -k c:\snKey.snk

Microsoft Visual C# .NET, Microsoft Visual C# 2005 ?? ?????? ??????? ?? ????? ?? Windows ?????????? ???????? ?? ?????

???????:?? ?????? ?????????????? ???????????? ?? ?????????????? ?????????? ???? ????? attack ???? ?? ??? ???? ???????? ?? ???? ??????? ?? ????? ???? ????????? ??? ?? ???? ??? ?? ?? ???? ?????? ?? ??????? ???? ???? ??? ????? ?? ??????? ?????? ?? ??? ??? ???? ?? ???? ????? ?? ????? ??? ?? ???? ?????? ?? ??????????? ?? ????.. ?? ???? ?????? ?? ???? ????? ?? ????? ?? ????? ????..

????????? ???? ?? ?? ?????????? ???????? ?? ?? ????? ???? ????AllowPartiallyTrustedCallers??? ??????? ?? ??????? ??? An assembly that has a strong name can only be called by a fully trusted caller unless the assembly uses theAllowPartiallyTrustedCallers??????? ??? The sample for the user control also demonstrates how to use theAssert?????? ??? TheAssertmethod declares that the calling code can use the code that calls theAssertmethod to access the resource that is protected by a permission demand. The code can access the resource even if callers that are higher in the stack have not been granted permission to access the resource.

This user control lets you select a file by using the open dialog box. ???????? ??? ??? ????? ??? ????? ?? ???? ????? ???? ??? ????? ??? ?? ?? ?? ???? ????? ?????????? ???????? ?? ?? ?????????? ???????? ????? ???? ??? ????? ????? ?? ?? ????? ?? ??? ????????? ???? ??, ?? ???? ????? ????? ?? ?? ????? ?? ??????? ?? ?????? ???

?? ????? ????? ?? ??? ???? ?? ???OpenFileDialog????? ???, ?? ????? ?? ?????? ?????? ?? ?? ???? ????? FileIOPermission ????? ?? ???? ?????????? ???????? ?? ?? ???? ??? ???? ?????? ?? ?? ?????? ???? ?????? ??? ???????? ?? ??? ???? ???? ??? ??? ?? ??? ?? ?????? ???? ???

To prevent a stack walk that is not successful because the caller does not have the required FileIOPermission permission type, use theAssert?????? ??? ?? ???Assertmethod can open security vulnerabilities if theAssertmethod is used incorrectly or inappropriately. Therefore, you must use theAssertmethod with great caution. ARevertAssertmethod must follow theAssertmethod as soon as the file operation is completed.

???:To make sure that the contents of the file appear correctly in the list box, use this user control to select only text files.
  1. Start Microsoft Visual Studio .NET, Microsoft Visual Studio 2005, or a later version of Visual Studio.
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????. The??? ?????????????? ?????,.
  3. ?? ???????????????? ??????????? ????,????? ????????? C#.

    ???:In Visual Studio, click????? C#?? ???????????????? ??????.
  4. ?? ???????????????????? ????,Windows Control Library.
  5. ????????????? ???,UserControl.
  6. ????? ????,OK.
  7. ?????? Explorer ????? ??? ???? ?????UserControl1.cs?? ????-????? ????, ?? ???? ???????? ???. The UserControl1.cs file appears.
  8. ?????? ??? ?? ????? ??? ?? ???????????? ????:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.IO;
    using System.Security;
    using System.Security.Permissions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    
    [assembly:AllowPartiallyTrustedCallers]
    
    namespace UserControl
    {
    	/// <summary>
    	/// Summary description for UserControl1.
    	/// </summary>
    	public class UserControl1 : System.Windows.Forms.UserControl
    	{
    		private System.Windows.Forms.TextBox textBox1;
    		private System.Windows.Forms.ListBox listBox1;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public UserControl1()
    		{
    			// This call is required by the Windows.Forms Form Designer.
    			InitializeComponent();
    
    			// TODO: Add any initialization after the InitForm call.
    			OpenFileDialog fileDialog = new OpenFileDialog();
    			if(fileDialog.ShowDialog() == DialogResult.OK)
    			{
    				// Reading the name of the selected file from the OpenFileDialog box
    				// and reading the file requires FileIOPermission.   
    				// The Assert command must be followed by a RevertAssert as soon as the file operation 
    				// is completed.
    				new FileIOPermission(PermissionState.Unrestricted).Assert();
    				textBox1.Text = fileDialog.FileName;
    				// Display the contents of the file in the text box.
    				FileStream fsIn = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, 
    					FileShare.Read);
    				StreamReader sr = new StreamReader(fsIn);
    			
    				// Process every line in the file.
    				for (String Line = sr.ReadLine(); Line != null; Line = sr.ReadLine()) 
    				{
    					listBox1.Items.Add(Line);
    				}
    				// It is very important to call RevertAssert to restore the stack walk for
    				// file operations.
    				FileIOPermission.RevertAssert();
    			}
    
    		}
    
    		/// <summary>
    		/// Clean up any resources that are being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if( components != null )
    					components.Dispose();
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Component Designer generated code
    		/// <summary>
    		/// Required method for Designer support. Do not modify 
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.textBox1 = new System.Windows.Forms.TextBox();
    			this.listBox1 = new System.Windows.Forms.ListBox();
    			this.SuspendLayout();
    			// 
    			// textBox1
    			// 
    			this.textBox1.Location = new System.Drawing.Point(32, 16);
    			this.textBox1.Name = "textBox1";
    			this.textBox1.TabIndex = 0;
    			this.textBox1.Text = "textBox1";
    			// 
    			// listBox1
    			// 
    			this.listBox1.Location = new System.Drawing.Point(144, 16);
    			this.listBox1.Name = "listBox1";
    			this.listBox1.Size = new System.Drawing.Size(120, 95);
    			this.listBox1.TabIndex = 1;
    			// 
    			// UserControl1
    			// 
    			this.Controls.Add(this.listBox1);
    			this.Controls.Add(this.textBox1);
    			this.Name = "UserControl1";
    			this.Size = new System.Drawing.Size(376, 120);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    			}
    }
    
  9. ?????? Explorer ???, ???? ?????AssemblyInfo.cs?? ????-????? ????, ?? ???? ???????? ???. The AssemblyInfo.cs file appears.
  10. ????? ??? ?? ?????? ?????:
    [assembly: AssemblyVersion("1.0.*")]
    Replace this code with the following code:
    [assembly: AssemblyVersion("1.0.0.0")]
  11. ????? ??? ?? ?????? ?????:
    [assembly: AssemblyKeyFile("")]
    Replace this code with the following code:
    [assembly: AssemblyKeyFile("c:\\snKey.snk")]
  12. ????? ????????????? ??,??????? ??????.

Create a code group to assign the permissions for the assembly

???????:?? ?????? ?????????????? ???????????? ?? ?????????????? ?????????? ???? ????? attack ???? ?? ??? ???? ???????? ?? ???? ??????? ?? ????? ???? ????????? ??? ?? ???? ??? ?? ?? ???? ?????? ?? ??????? ???? ???? ??? ????? ?? ??????? ?????? ?? ??? ??? ???? ?? ???? ????? ?? ????? ??? ?? ???? ?????? ?? ??????????? ?? ????.. ?? ???? ?????? ?? ???? ????? ?? ????? ?? ????? ????..

A code group determines whether an assembly matches administrator-defined criteria that is referred to as a membership condition. If the assembly matches, the code group grants the assembly a set of permissions that has been associated with that code group.

To create a code group:
  1. ????? ????,????????? ????? ????,?????????? ????-????? ????, ?? ???? ??????????? ????. The???????? ????window appears.
  2. ???-????? ???????????????? ?????. The???????????? ?????window appears.
  3. In Microsoft Visual Studio .NET 2002, double-clickMicrosoft .NET Framework Configuration. The.NET Framework Configurationwindow appears.

    For Microsoft Visual Studio .NET 2003, double-clickMicrosoft .NET Framework 1.1 Configuration. The.NET Configuration 1.1window appears.

    In Microsoft Visual Studio 2005 or in later versions, double-clickMicrosoft .NET Framework 2.0 Configuration. The.NET Framework 2.0 Configurationwindow appears.
  4. ???? ??? ??? ??????? ?????????? ??????? ????, ??????? ????????, ?? ???? ??? ??????? ??????? ????.
  5. ????-????? ????All_Code?? ????-????? ????, ?? ???? ??????. The Create Code Group wizard appears.
  6. ????????? ???? ?? ??Create a new code groupis selected, typeMyUserControlCodeGroup????????????? ???, ?? ???? ???next. The??? ???? ?????? ?????page appears.
  7. ??????? ???? ?? ??? ?? ??? ?????? ?????? ?????????, ????? ????Strong Name.
  8. ????? ????,???? ????. TheImport Strong Name From Assembly????? ?????,.
  9. Locate the UserControl.dll file that you created in the "Create a Windows user control by using Microsoft Visual C# .NET, Microsoft Visual C# 2005, or a later version" section, and then click?????.
  10. ????? ????,next. TheAssign a Permission Set to the Code Grouppage appears.
  11. ????? ????,?????? ?????? ??? ?? ????? ??????? ????FullTrust????, ?? ???? ??? ??next. TheCompleting the Wizardpage appears.
  12. ????? ????,??????to close the Create Code Group wizard.

Create an HTML file to call the user control

?? ??????? ?? ??? ????????? ????? ???? ?? ??? ??? ??? ???? ????? ???, ?? ??? ??????? ?? ?????????? ???????? ?? ??? ???? ?? ??? ???? HTML ???? ????? ????? ??, ?? ?? ??? ?? ???? ?????? ??????? ????????? ???? ?? ?????????? ???????? ??????????? ??? ???? ???
  1. ????? ????,???????????? ????,???????????:???????? ????-????? ????, ?? ???? ???OK.
  2. Notepad ???, ????? ??? ???????:
    <OBJECT id="MyWinControl1" height="200" width="200" classid="http:UserControl.dll#UserControl.UserControl1" VIEWASTEXT>
        
    </OBJECT> 
  3. Microsoft ??????? ????? ?? ??? ??????? ??? ???????? (IIS), CallUserControl.htm ?? ??? ??? ????? ???????
  4. IIS ??? ??????? ??? "????? Windows ?????????? ???????? ?? Microsoft Visual C# .NET, Microsoft Visual C# 2005 ?? ?????? ??????? ?? ????? ???? ???" ??? ??? ???? ?????? ???? ?? UserControl.dll ????? ?? ????????? ??????
  5. Open Microsoft Internet Explorer.
  6. ????????????? ???,http://localhost/CallUserControl.htm???? ????, ?? ???? ??? ENTER ?????.. The?????????? ?????,.
  7. Locate any text file, and then click?????. The text of the file appears in theListBoxcontrol on the browser.

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ?????? ?? ????::
Configuring code groups using the .NET Framework Configuration tool
http://msdn2.microsoft.com/en-us/library/eb8xw74a(vs.71).aspx

CodeAccessPermission.Assert method
http://msdn2.microsoft.com/en-us/library/system.security.codeaccesspermission.assert(vs.71).aspx

.NET Framework assemblies and the AllowPartiallyTrustedCallers attribute
http://msdn2.microsoft.com/en-us/library/aa302328.aspx

AllowPartiallyTrustedCallersAttribute class
http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute(vs.71).aspx

Assembly security considerations
http://msdn2.microsoft.com/en-us/library/ab4eace3(vs.71).aspx

Security briefs: Strong names and security in the .NET Framework
http://msdn2.microsoft.com/en-us/library/aa302416.aspx

???

???? ID: 839300 - ????? ???????: 05 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2008 Express Edition
??????: 
kbhowtomaster kbhowto kblistbox kbfileio kbdll kbcontrol kbweb kbuser kbsecurity kbpolicy kbopenfile kbmt KB839300 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:839300

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com