简体   繁体   English

如何在Internet Explorer的网页上从javascript调用.Net控件或类库?

[英]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. 我正在开发一个Intranet应用程序,它需要从客户端计算机收集来自环境变量信息的系统信息,以便它可以在以后由服务器处理时使用。 The browser is Internet Explorer, versions 8 and 9. 浏览器是Internet Explorer,版本8和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. 我认为Silverlight是一种可能性,但它似乎太过限制(出于安全原因)能够获取我需要收集的信息。

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. 实际上,您可以编写.Net类并在IE中从JavaScript实例化它。

  1. Make your class ComVisible 让你的班级ComVisible
  2. Give your class a ProgId 给你的班级一个ProgId
  3. Register the .Net assembly with regasm 使用regasm注册.Net程序集
  4. In your JavaScript code, call new ActiveXObject 在JavaScript代码中,调用new ActiveXObject

Anyway, there is still a security issue: the user will have to manually allow the ActiveX execution. 无论如何,仍然存在安全问题:用户必须手动允许ActiveX执行。

As I remember, it should be something like that (not tested): 我记得,它应该是那样的(没有经过测试):

In C#: 在C#中:

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

In administrator console: 在管理员控制台:

regasm MyClass.dll

In JavaScript: 在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. 由于浏览器是为Internet设计的,因此为他们提供读取任何类型系统设置的能力会导致安全问题。 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. 但是,您可以考虑编写一个基于Windows的示例应用程序,该应用程序从您的Intranet网站下载,读取环境设置并提交到服务器。

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. 然后,您将在浏览器中需要时从数据库中提取信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Javascript在ASP.Net页面上调用静态方法? - How do I call a static method on my ASP.Net page, from Javascript? 如何使用 Z686155AF75A60A0EDD3F6E9D80C1 从 ASP.NET Controller 重定向到另一个 web 页面? - How do I redirect to another web page from an ASP.NET Controller using JavaScript? 如何在Internet Explorer或.NET Webbrowser控件中阻止iframe - How to blocking iframes in internet explorer or .NET webbrowser control 如何打开最大化的Internet Explorer? - How do I open maximized internet explorer? 从ASP.Net用户控制事件调用页面上的javascript - Call javascript on page from ASP.Net User Control Event 在托管WebBrowser控件的应用程序中,如何在由WebBrowser控件查看的页面中调用JavaScript函数? - How might I call a JavaScript Function in a Page Viewed by a WebBrowser Control from the Application Hosting the WebBrowser Control? 从asp.net页在Internet Explorer中查看xml文件 - View xml file in Internet Explorer from an asp.net page 如何在web.config中更改Internet Explorer的“文档模式”? - How do I change the 'Document Mode' of Internet Explorer in web.config? asp.net 用Internet Explorer登录控制 - asp.net login control with internet explorer 如何在ASP .NET的Web窗体上的控件中实现CSS类? - How do I implement a CSS class in a control on my web form in ASP .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM