How to use the. NET ie winform control.

Source: Internet
Author: User
I believe many people have read Lu Yan's article about how to use XML to print general web reports (implementation ). Article , The method is very good. However, this method is difficult to use because it actually uses the Windows form control.

The winform control is indeed a good thing. It can greatly enhance IE's functions on the client, but many people do not use it much. Why? Security issues.

. NET Framework provides a much more powerful security model than ActiveX controls. I am not going to talk about. Net Security here. the. NET Framework SDK documentation provides a detailed explanation. Here is an example.

Open vs.net 2003 and create a Windows control library project named namespace. Client. Then, change the automatically generated class name to testform. In the design view, drag a button control from the toolbox and place it on the control form.

Create a new windows form from the project in Solution Explorer. The form name is casual. Here is popform. Add a button in the form and add a sentence in the Click Event of the button.Code:This. Close ();

Return to the testform control design interface and double-click the added form button to view the specific code. Add the following private member definition to the class declaration:PrivatePopform pform= Null;

Then, click the button to write the following code in the event definition, assuming that the button ID is btnopen: Private   Void Btnopen_click ( Object Sender, system. eventargs E)
{
If ( This . Pform =   Null )
{
Pform= NewPopform ();
Pform. showdialog ();
}
Else
Pform. showdialog ();
}

The code above is very simple, that is, to create a winform control, add a button in the control, this button opens a form named popform, there is a button in the form of popform, responsible for closing.
After compilation, A. Net file named namespace. Client. dll is generated.ProgramSet.

Add this Assembly to the ASP. NET page. The Code is as follows:<ObjectClassid= "Http://www.host.com/global/Sanxing.WinControls.dll#Sanxing.WinControls.ClientTest"Viewastext> </Object>

In fact, you do not need to use. aspx. However, you must note that you cannot use the local localhost as the test site, because the local localhost site has the editing/management permissions by default and is a member of the Administrator group, the control is displayed normally. The solution is to test it on a remote web server. If there is no remote server, you can simulate one on the Local Computer and add several IP addresses to the NIC of the Local Computer, such as 192.168.0.2, 192.168.0.3, then, use the DNS service on the local computer to bind 192.168.0.2 to a private domain name, such as www.host.com, and then create a new web site in IIS to bind 192.168.0.2 to this site, then you can access the site on the local machine through www.host.com.

Now, execute this page to make sure that all interfaces of this control cannot be displayed. Nothing may be displayed, or a Red Cross may appear in a box .. Net runtime may pop up a dialog box, indicating that the control does not have any permissions. In fact, this result is caused by Microsoft's default settings. The author must be in the assemblyinfo of the control where the control is located. run a security statement in CS/Vb to declare that the control must use the granted permissions to display the interface.

Let's take a look at the permissions defined for the Internet Zone in. Net Security Configuration. This area defines what can be done by downloading/executing an assembly on an Internet webpage.
Open Control Panel --> Administrative Tools --> Microsoft. net Framework 1.1 configuration --> "running database security policy" --> "computer" --> "code group" --> "all_code" --> "internet_zone ", click "edit code group attributes" and switch to the "permission set" tab. The following list lists the permissions that can be granted to the Assembly downloaded from the Internet. For example:

It can be seen that the Assembly executed on the internet can have an interface, pop-up dialog box and print, but why can't the winform control we just wrote be displayed in IE?

The reason is that we do not declare the required permissions in the control. When no permission is declared in the code ,.. Net runtime security management will not know what permissions we want to use, so security management will assign the default "No permissions" to this control, so this control does not have the permissions to the display interface, of course it cannot be displayed.
Therefore, you can use a special security attribute declaration in the control. This statement indicates that the control requires the control permission of the client and cannot exceed. net default permissions, then. net. In this way, the control is displayed.

This statement is added to assemblyinfo. CS:[Assembly: allowpartiallytrustedcallers ()]

The above Code indicates that this Assembly requires the default permission defined in. Net Security Settings and does not exceed this default permission.

When the client. Net Runtime Library executes the code of this control, it detects this declaration and grants the internet_zone permission to this control. In this way, the space is displayed.

However, this method is flawed, that is, you can only use the default permissions set by. net. The client cannot access the file, the Registry, or the database. You cannot do anything except the display window. In addition, the displayed window has such a warning.

Indicates that the code that can be executed in this window is restricted.

This method is actually good, because the main purpose of the winform control is to display the window on the client, and the most important thing is to access the printer. If there is no special purpose, this method can be used on the Internet's public website, you can give it to everyone, as long as your client has installed the. NET Runtime Library.

However, to perform advanced operations on the client, you need more permissions, such as accessing the registry, accessing the database, and performing network operations. This method is not applicable because it has crossed the default. net settings and must be used in other ways to bypass. Net's default security settings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.