开发SharePoint Server 2019 WebPart时找不到SecureStoreProvider类
一、先确认Secure Store Service的基础部署状态
- 打开SharePoint管理中心,进入应用程序管理 -> 管理服务应用程序,检查是否存在Secure Store Service应用程序且状态为运行中。
- 用PowerShell验证服务实例状态:
如果状态不是Get-SPServiceInstance | Where-Object {$_.TypeName -eq "Secure Store Service"}Online,启动对应的服务实例。
二、正确引用程序集(避免手动找DLL)
- 不要直接在磁盘搜索DLL,通过Visual Studio的GAC引用更可靠:
- 在项目中右键添加引用 -> 选择程序集 -> 框架 -> 扩展,找到
Microsoft.Office.SecureStoreService.Server(版本16.0.0.0)并添加。 - 如果扩展列表中没有,直接浏览到GAC路径
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Office.SecureStoreService.Server,选择对应版本的DLL。
- 在项目中右键添加引用 -> 选择程序集 -> 框架 -> 扩展,找到
- 若GAC中也没有该程序集,说明SharePoint安装时未包含Secure Store组件:
- 打开控制面板 -> 程序和功能,找到
Microsoft SharePoint Server 2019,点击更改。 - 在安装向导中选择添加或删除功能,确保勾选Secure Store Service组件,完成安装后运行
PSConfig.exe更新配置。
- 打开控制面板 -> 程序和功能,找到
三、服务器端访问Secure Store的代码示例
如果已正确引用程序集,使用以下代码访问Secure Store(符合SharePoint最佳实践,替代直接实例化SecureStoreProvider的方式):
using Microsoft.Office.SecureStoreService.Server; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; public SecureStoreCredentialCollection GetSecureStoreCredentials(string targetAppId) { if (string.IsNullOrEmpty(targetAppId)) throw new ArgumentNullException(nameof(targetAppId)); // 获取Secure Store服务代理 var secureStoreProxy = SPFarm.Local.ServiceProxies.GetValue<SecureStoreServiceProxy>(); if (secureStoreProxy == null) throw new InvalidOperationException("Secure Store Service代理未找到"); // 获取Secure Store操作实例 using (var secureStore = secureStoreProxy.GetSecureStore(SPContext.Current.Site)) { // 验证目标应用程序是否存在 if (!secureStore.DoesApplicationExist(targetAppId)) throw new InvalidOperationException($"目标应用程序 {targetAppId} 不存在"); // 获取凭据集合 return secureStore.GetCredentials(targetAppId); } }
四、补丁与配置排查
- 重新运行SharePoint配置向导(
PSConfig.exe):补丁安装后可能需要执行配置更新,才能让程序集生效。 - 检查已安装的SharePoint累积更新:确保所有后续累积更新已安装,部分补丁可能调整了程序集的位置或命名。
内容的提问来源于stack exchange,提问作者Andrew Malcolm
相关产品推荐
相关产品推荐

