简体   繁体   中英

How do I call a .Net control or class library from javascript on a web page in Internet Explorer?

I am working on an intranet app where it needs to collect system information from environment variable information from the client machine so it can be used in later processing by the server. The browser is Internet Explorer, versions 8 and 9.

I looked at Silverlight as a possibility but it appears to be too constrained (for security reasons) to be able to get at the information I need to collect.

So far, searching for an answer hasn't yielded anything yet. Any thoughts?

Indeed, you can write a .Net class and instanciate it from JavaScript in IE.

  1. Make your class ComVisible
  2. Give your class a ProgId
  3. Register the .Net assembly with regasm
  4. In your JavaScript code, call new ActiveXObject

Anyway, there is still a security issue: the user will have to manually allow the ActiveX execution.

As I remember, it should be something like that (not tested):

In C#:

[ComVisible(true)]
[ProgId("MyCompany.MyClass")]
public class MyClass
{
    public string UserName
    {
        get { return Environment.UserName; }
    }
}

In administrator console:

regasm MyClass.dll

In JavaScript:

var myClass = new ActiveXObject("MyCompany.MyClass");
alert(myClass.UserName);

I don't think one can read environment variables from within browsers. Since browsers are designed for the Internet, providing them with an ability to read system's settings of any kind would lead to security issues. Nobody wants a website he/she visiting on the Internet reading his system information right ? I am very skeptical about the possibility of doing the thing you are trying to achieve. However, you might consider writing a sample windows based app, that gets downloaded from your intranet website, reads the environment settings and submits to the server.

This should not be possible to do as it is no ones interest(except the bad guys)that you can write programs that access your local computer.

I don't know how many users you have. But if you definitely need the information, one solution would be to create a program that pulls all the needed data from the computers and stores it in a database mapped to the user. Then you will pull the information from the database when you need it in the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM