使用microsoft.interope.word从ASP.Net应用程序拒绝对Office的访问

问题描述 投票:0回答:1

我正在为ERP系统工作,我们将文档发送给我们的办公室员工以对文档进行数字签名,为此,我们希望读取.docx文件,并使用Microsoft.Office.Interop.Word参考。

以下是我尝试过的代码:

            string allText = "";
            // if word file
            if (fileExtension == ".docx" || fileExtension == ".doc")
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                stopwatch.Stop();
                object miss = System.Reflection.Missing.Value;
                object readOnly = true;
                object Path = filePath;
                // getting the document
                Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref Path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

                // if document contains at least single line of signature
                bool docHasSignature = doc.Content.Find.Execute("{{signature_");
                if (docHasSignature)
                {
                    // reading all the and appending in the string
                    allText += (doc.Content.Text.ToString());
                }

                //signatureCount = 3;
            }

当我尝试从用户端运行此代码并请求此方法时,它给出以下错误:

由于以下错误,为具有CLSID {000209FF-0000-0000-C000-000000000046}的组件检索COM类工厂失败:80070005拒绝访问。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。

我还尝试了此链接上提供的步骤https://docs.microsoft.com/en-us/windows/win32/wmisdk/securing-a-remote-wmi-connection?redirectedfrom=MSDN但仍然出现相同的结果,如果有人指导我,这将是非常感激的行为。

c# office-interop
1个回答
0
投票

改为使用Open XML SDK

Considerations for server-side Automation of Office页面指出以下内容:

Microsoft当前不建议,并且不支持从任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)自动化Microsoft Office应用程序,因为Office可能表现出不稳定的行为和/或在此环境中运行Office时出现死锁。

如果要构建在服务器端上下文中运行的解决方案,则应尝试使用已确保安全进行无人值守执行的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。如果您从服务器端解决方案中使用Office应用程序,则该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。

本文介绍了使用Office自动化的可能替代方法。

© www.soinside.com 2019 - 2024. All rights reserved.