Article ID: 839300 - Last Review: July 15, 2008 - Revision: 4.1 How to use the AllowPartiallyTrustedCallers attribute to call an assembly that has a strong name from a Web page by using Visual C# .NET, Visual C# 2005 or later versionsImportant This article contains information that shows you how to help
lower security settings or how to turn off security features on a computer. You
can make these changes to work around a specific problem. Before you make these
changes, we recommend that you evaluate the risks that are associated with
implementing this workaround in your particular environment. If you implement
this workaround, take any appropriate additional steps to help protect your
system. On This PageSUMMARYYou may want to call a Windows user control from a Web page. To call a Windows user control that can access secured resources and that is built as an assembly that has a strong name, you must mark the assembly of the Windows user control with the AllowPartiallyTrusted assembly attribute. You must also include a call to the Assert method to allow the Windows user control to access the secured
resource. INTRODUCTIONThis step-by-step article describes how to call a Windows
user control from a Web page. The Windows user control that this article
describes is built as an assembly that has a strong name. The sample in this
article describes how to use the AllowPartiallyTrustedCallers attribute of the assembly so that an assembly that has a strong
name can be accessed from a Web page. The sample in the article also describes
how to use the Assert method. Create a key pair that has a strong nameA key pair that has a strong name is used to sign an assembly for a user control that has a strong name. The strong name is used when you create a code group that grants permission to use the assembly from partially trusted code.
Create a Windows user control by using Microsoft Visual C# .NET, Microsoft Visual C# 2005, or a later versionWarning This workaround may make your computer or your network more vulnerable to attack by malicious users or by malicious software such as viruses. We do not recommend this workaround but are providing this information so that you can implement this workaround at your own discretion. Use this workaround at your own risk.This user control demonstrates how to use the AllowPartiallyTrustedCallers attribute of an assembly. An assembly that has a strong name can only be called by a fully trusted caller unless the assembly uses the AllowPartiallyTrustedCallers attribute. The sample for the user control also demonstrates how to use the Assert method. The Assert method declares that the calling code can use the code that calls the Assert method 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. The control then opens the text file in the list box. The user interface of this user control includes one text box and one list box. The text box displays the name of the file that is selected, and the list box shows the contents of the file that is selected. To read the name of the selected file from the OpenFileDialog box, and to read the file, the FileIOPermission permission type must be granted. The user control must have this permission granted through its code group. The Web page that calls the control does not have this permission. To prevent a stack walk that is not successful because the caller does not have the required FileIOPermission permission type, use the Assert method. Note that the Assert method can open security vulnerabilities if the Assert method is used incorrectly or inappropriately. Therefore, you must use the Assert method with great caution. A RevertAssert method must follow the Assert method as soon as the file operation is completed. Note To make sure that the contents of the file appear correctly in the list box, use this user control to select only text files.
Create a code group to assign the permissions for the assemblyWarning This workaround may make your computer or your network more vulnerable to attack by malicious users or by malicious software such as viruses. We do not recommend this workaround but are providing this information so that you can implement this workaround at your own discretion. Use this workaround at your own risk.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:
Create an HTML file to call the user controlAfter you create a code group to assign the permissions for the assembly, you must create an HTML file to call the user control from the browser, and you must set up the environment to make sure that the user control is called successfully.
REFERENCESFor more information, visit the following Microsoft
Developer Network (MSDN) Web sites: Configuring code groups using the .NET Framework Configuration tool http://msdn2.microsoft.com/en-us/library/eb8xw74a(vs.71).aspx (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 (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 (http://msdn2.microsoft.com/en-us/library/aa302328.aspx) AllowPartiallyTrustedCallersAttribute class http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute(vs.71).aspx (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 (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 (http://msdn2.microsoft.com/en-us/library/aa302416.aspx) APPLIES TO
| Article Translations
|
Back to the top
