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

时间:2011-03-03 00:55:48

标签: sharepoint sharepoint-2010 webdav credentials office-interop

使用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的内容吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试以用户名和密码的用户身份执行模拟代码?有很多例子如何在网上做到这一点,例如http://www.codeproject.com/KB/dotnet/UserImpersonationInNET.aspx。我现在无法自己测试,但它应该有用......

答案 1 :(得分:0)

假设您有Windows 8.1。 Office使用的凭据存储在Windows凭据管理器中。通过转到开始>访问它搜索"凭证管理器" >然后单击Windows Credentials。

凭证应与以下内容类似:

MicrosoftOffice15_Data:orgid:<emailadress>

使用此api http://credentialmanagement.codeplex.com,您可以使用以下方法在Windows Credential Store中自行存储凭据:

var cm = new Credential { Target = "MicrosoftOffice15_Data:orgid:youremailhere@email.com", PersistanceType = PersistanceType.LocalComputer, Password = "yourverysecurepasswordhere" };
cm.Save();

然后,当您使用Word打开文档时,它会打开它而不会提示您输入凭据。

使用Office 2013和Windows 8.1在SharePoint Online上测试。