将凭据传递给 Word Interop 以在 SharePoint 中打开文档

发布于 2024-10-20 07:37:26 字数 1137 浏览 3 评论 0原文

正在开发 WPF 应用程序 (C# 4.0)。

系统在SharePoint中存储了大量的Word文档。 Word 可以直接编辑这些文档,因为 SharePoint 通过 WebDAV 将文档库公开给 Office。

为了启动 Word 来编辑这些文档,我们的 WPF 应用程序使用 Microsoft.Office.Interop.Word。

发现(通过简单尝试)使用 Interop.Word 打开本地文档与通过 WebDAV 从 SharePoint 打开文档之间的唯一区别是您传递的 ref 对象 FileName 是 URL 而不是本地路径字符串。

这一切都正常:

var wordApplication = 
    new Microsoft.Office.Interop.Word.Application { Visible = true };

object filePath = 
    "http://PathToSharepoint.com/DocumentLibrary/DocumentName.doc";

object missing = Missing.Value;
object readOnly = false;
object isVisible = true;

Document reportDoc = wordApplication.Documents.Open(
    ref filePath,
    ref missing,
    readOnly,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref isVisible);

reportDoc.Activate();

但是,如果运行应用程序的 Windows 用户不是具有文档库权限的域用户,Word 最多会提示他们输入用户名和密码,最坏的情况有时只是抛出 COMException。

用户已经使用他们需要提供的相同凭据登录到我们的 WPF 应用程序,并且我们在内存中拥有用户名和 securePassword。但是,我看不出有任何明显的方法可以向 Word 提供这些凭据。

有人知道我如何提供类似于 NetworkCredential 的东西吗?

Working on a WPF application (C# 4.0).

The system stores a number of Word documents in SharePoint. Word can directly edit those documents, as SharePoint exposes document libraries to Office via WebDAV.

In order to launch Word to edit these documents, our WPF app uses Microsoft.Office.Interop.Word.

Discovered (through simply trying it) that the only difference between using Interop.Word to open a local document vs. a document from SharePoint via WebDAV is that the ref object FileName you pass is the URL instead of a local path string.

This all just works:

var wordApplication = 
    new Microsoft.Office.Interop.Word.Application { Visible = true };

object filePath = 
    "http://PathToSharepoint.com/DocumentLibrary/DocumentName.doc";

object missing = Missing.Value;
object readOnly = false;
object isVisible = true;

Document reportDoc = wordApplication.Documents.Open(
    ref filePath,
    ref missing,
    readOnly,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref isVisible);

reportDoc.Activate();

HOWEVER, if the Windows user running the application isn't a domain user with permissions to the document library, at best Word will prompt them for a username and password, and at worst sometimes just throw a COMException.

The user has already logged into our WPF app with the same credentials that they would need to supply, and we have the username and securePassword in memory. However, I can't see any obvious way of how you'd supply those credentials to Word.

Anyone got any clue how I could provide something akin to a NetworkCredential to this?

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

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

发布评论

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

评论(2

醉南桥 2024-10-27 07:37:26

您是否尝试以您拥有用户名和密码的用户身份来执行代码?网上有很多如何执行此操作的示例,例如 http://www.codeproject。 com/KB/dotnet/UserImpersonationInNET.aspx。我现在无法亲自测试它,但它应该有效......

Did you try to execute the code impersonated as the user whose user name and password you have? There are many examples how to do that on the web, e.g. http://www.codeproject.com/KB/dotnet/UserImpersonationInNET.aspx. I can't test it right now myself, but it should work...

流云如水 2024-10-27 07:37:26

假设您有 Windows 8.1。 Office 使用的凭据存储在您的 Windows 凭据管理器中。通过转到“开始”>“访问此内容”搜索“凭据管理器”>然后单击“Windows 凭据”。

凭据应类似于以下内容:

MicrosoftOffice15_Data:orgid:<emailadress>

使用此 api http://credentialmanagement.codeplex.com 您可以存储您自己在 Windows 凭据存储中使用以下凭据:

var cm = new Credential { Target = "MicrosoftOffice15_Data:orgid:[email protected]", PersistanceType = PersistanceType.LocalComputer, Password = "yourverysecurepasswordhere" };
cm.Save();

然后,当您使用 Word 打开文档时,它会在不提示您输入凭据的情况下打开该文档。

在使用 Office 2013 和 Windows 8.1 的 SharePoint Online 上进行了测试。

Assuming you have windows 8.1. The credentials used by Office are stored in your Windows Credential Manager. Access this by going to Start > Search for "Credential Manager" > then click on Windows Credentials.

The credential should look similar to this:

MicrosoftOffice15_Data:orgid:<emailadress>

Using this api http://credentialmanagement.codeplex.com you are able to store a credential yourself in the Windows Credential Store using:

var cm = new Credential { Target = "MicrosoftOffice15_Data:orgid:[email protected]", PersistanceType = PersistanceType.LocalComputer, Password = "yourverysecurepasswordhere" };
cm.Save();

Then when you open the document using Word for example it opens it without prompting for your credentials.

Tested on SharePoint Online with Office 2013 and Windows 8.1.

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