通过网页上的按钮打开 Word 应用程序

发布于 2024-09-01 03:53:37 字数 1561 浏览 4 评论 0原文

我正在开发一个概念验证 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-09-08 03:53:37

使用 .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.

北陌 2024-09-08 03:53:37

帖子如何为 Internet Explorer 托管的程序集提供额外的信任< .NET 安全博客 中的 /a> 阐明了该问题。它的日期是 2003 年,所以现在情况可能已经改变了……我不知道。

但评论者询问 (2006)

是否可以执行.net
具有所有信任权限的组装
无需更改任何内容
客户端?我们之前曾
在 CAB 中使用签名的 ActiveX
工作正常,并尝试移植它
到 C#。

肖恩法回答了

不,目前不可能
提升您在客户端的权限
一侧用于控制。最接近的
选项是 ClickOnce 这将允许
您提示并提升
应用程序——尽管这
应用程序不会被冲洗在
网页。

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)

Is it possible to execute the .net
assembly with all the trust permission
without changing anything on the
client side? We previously have been
using a signed ActiveX in a CAB that
was working fine, and try to port it
to C#.

And Shawnfa answered

No, it is not currently possible to
elevate your permissions on the client
side for a control. The closest
option is ClickOnce which will allow
you to prompt and elevate an
application -- although this
application will not be hosed in the
web page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文