通过网页上的按钮打开 Word 应用程序
我正在开发一个概念验证 Web 应用程序:一个带有按钮的网页,可打开用户 PC 上安装的 Word 应用程序。
我被 Visual Studio 2008 Express 中的 C# 项目困扰(Windows XP 客户端,LAMP 服务器)。我遵循了 在 .NET 中编写 ActiveX 控件 教程,经过一些调整后效果很好。然后我添加了打开 Word 的按钮。
问题是我可以从项目中引用 Microsoft.Office.Interop.Word,但无法从网页访问它。该错误显示“该程序集不允许部分受信任的调用者”。
我读过很多有关 .NET 安全性的文章,但现在我完全迷失了。免责声明:我从 4 天前开始就开始关注 .NET。
我试图解决这个问题,但我看不到曙光! 我什至不知道这是否永远可能!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Security.Permissions;
using System.Security;
[assembly: AllowPartiallyTrustedCallers]
namespace OfficeAutomation
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void openWord_Click(object sender, EventArgs e)
{
try
{
Word.Application Word_App = null;
Word_App = new Word.Application();
Word_App.Visible = true;
}
catch (Exception exc)
{
MessageBox.Show("Can't open Word application (" + exc.ToString() + ")");
}
}
}
}
I'm developing a proof of concept web application: A web page with a button that opens the Word Application installed on the user's PC.
I'm stuck with a C# project in Visual Studio 2008 Express (Windows XP client, LAMP server). I've followed the Writing an ActiveX Control in .NET tutorial and after some tuning it worked fine. Then I added my button for opening Word.
The problem is that I can reference the Microsoft.Office.Interop.Word from the project, but I'm not able to access it from the web page. The error says "That assembly does not allow partially trusted callers".
I've read a lot about security in .NET, but I'm totally lost now. Disclaimer: I'm into .NET since 4 days ago.
I've tried to work around this issue but I cannot see the light!!
I don't even know if it will ever be possible!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Security.Permissions;
using System.Security;
[assembly: AllowPartiallyTrustedCallers]
namespace OfficeAutomation
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void openWord_Click(object sender, EventArgs e)
{
try
{
Word.Application Word_App = null;
Word_App = new Word.Application();
Word_App.Visible = true;
}
catch (Exception exc)
{
MessageBox.Show("Can't open Word application (" + exc.ToString() + ")");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 .Net Framework 4 + XBAP 使这一切变得简单:您可以使用 WPF XBAP 而不是 ActiveX。
在项目设置窗口中执行以下操作:
签名:取消勾选所有框。 (本项目无需签字),
在“安全”选项卡下,只需将其更改为“完全信任”即可。
如果用户想要允许应用程序运行,系统将提示一次。
Using .Net Framework 4 + XBAP makes this easy: You could use WPF XBAP instead of ActiveX.
And on Project settings window do:
Signing: unckeck all boxes. (this project does not need to be signed),
under Security tab, Just change it to Full Trust.
The user will be prompted one time if he wants to allow the application to Run.
帖子如何为 Internet Explorer 托管的程序集提供额外的信任< .NET 安全博客 中的 /a> 阐明了该问题。它的日期是 2003 年,所以现在情况可能已经改变了……我不知道。
但评论者询问 (2006)
肖恩法回答了
The post How to provide extra trust for an Internet Explorer hosted assembly in the .NET Security Blog sheds light on the issue. It's dated 2003 so things could have changed now... I don't know.
But a commenter asked (2006)
And Shawnfa answered