在 ASP.NET 中,如何检测 Word 2003 或 2007 文件中的密码?

发布于 2024-08-29 06:50:58 字数 121 浏览 2 评论 0原文

我的用户喜欢将受密码保护的 Word 文档上传到我们的自定义文档管理系统中。我想添加一个验证来检查密码,如果有密码则拒绝上传。

使用 COM 互操作实现 Word 自动化是不可能的,因为这是一个服务器端应用程序。

My users like to upload password-protected Word documents into our custom document management system. I'd like to add a validation to check for the password and refuse the upload if it has a password.

Automating Word - with COM interop - is out of the question because this is a server side application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半透明的墙 2024-09-05 06:50:58

我认为这将取决于 Microsoft Word 的版本。旧版本的 Word(2007 年之前)将需要一些 COM 互操作,因为它们不以开放格式保存。你无能为力解决这个问题,而且我能感受到你在网络服务器上安装任何 Office 时的挫败感。此外,我相信检测这些密码保护的唯一方法是尝试打开/取消保护文件并捕获特定异常(您可能还必须进一步评估异常中的错误代码)。不漂亮!

但是,对于以开放 DOCX 格式保存的较新版本的 word (2007+) (标准 ECMA-376),您可以检查 XML 并检查 DocumentProtection 元素是否存在,并将 w:enforcement 属性设置为“on”...

<w:DocumentProtection
   w:edit="read-only"
   w:enforcement="on"
   w:unprotectPassword="1FC6CBEB"/>

注意:这里看到的密码是加密的(显然);我用密码“test”保存了这个特定的文档。

I think it is going to depend on the version of Microsoft Word. Older versions of Word (before 2007) will require some COM interop, because they are not saved in an open format. Nothing you can do to get around that, and I feel your frustrations with installing anything Office on a web server. Additionally, I believe the only way to detect password protection on these is to attempt to open/unprotect the file and catch a certain exception (you may have to further evaluate an error code within the exception as well). Not pretty!

However, for newer versions of word (2007+) saved in the open DOCX format (Standard ECMA-376), you can examine the XML and check for the existence of the DocumentProtection element with the w:enforcement attribute set to "on"...

<w:DocumentProtection
   w:edit="read-only"
   w:enforcement="on"
   w:unprotectPassword="1FC6CBEB"/>

Note: The password seen here is encrypted (obviously); I saved this particular document with a password of "test".

孤凫 2024-09-05 06:50:58

不幸的是,它不能直接从 .NET 或 DSOFile.dll,但您可以在 .NET 中创建一个包装器来读取 PIDSI_DOC_SECURITY 任何 Office 文件的属性,无需打开文档即可查明该文件是否受密码保护。有许多 C++ 示例,可以使用 < 的定义进行移植代码>iPropertyStorage。

包装示例位于 TechTarget 上,但是包装器似乎不可用。

Unfortunately it's not available directly from .NET or DSOFile.dll, but you could create a wrapper in .NET to read the PIDSI_DOC_SECURITY property of any Office file to find out if it is password-protected or not without opening the document. There are a number of C++ samples out there that could be ported with a definition of iPropertyStorage.

A wrapped example is on TechTarget, but the wrapper seems to be unavailable.

妳是的陽光 2024-09-05 06:50:58

您是说您想在不使用任何 Office 互操作的情况下打开/检查 Word 文档吗?祝你好运!

可能您唯一的起点就是自己手动解析 docx (规范已发布 此处)。但实际上,我建议您硬着头皮使用 Office 互操作库,因为这比您自己解析文档的要求要轻得多。本质上没有什么可以阻止您在服务器应用程序上执行此操作。

Are you saying that you'd like to open/examine Word documents without using any Office interop at all? Good luck!

Possibly your only place to start would be to manually parse the docx yourself (the spec is published here). Really though, I'd suggest you bite the bullet and use the Office interop libraries, 'cause that's a much lighter weight requirement than parsing the document yourself. There's nothing that intrinsically prevents you doing that on a server application.

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