Sharepoint 2013 Secure Store Service
SharePoint Secure Store Service 在實際案例中的應用
文章目錄:
之前有一篇部落格講到使用EMSManagedAPI操作Exchange郵箱,在實際SharePoint開發中,使用者總是會要求在SharePoint中整合現有exchange郵件系統。在整合的過程中,如何讀取、新建、更改郵件(任務、會議)是首先要解決的問題,接下來要解決的是如何通過使用者驗證,也就是我們經常講到的單點登陸。由於owa採用表單驗證的方式,且沒有找到很好的SharePoint單點登陸有效方案,所以採用了SharePoint2010以後引入的SSS(Secure Store Service),07版本是SSO。下面就來介紹一下SharePoint Secure Store Service 在實際案例中的應用。
Secure Store Service 是SharePoint Server提供的執行在應用程式伺服器上的授權服務。Secure Store Service將使用者憑據儲存於一個憑據的資料庫,錄入使用者資訊時,會使用金鑰加密。使用者憑據通常包含使用者名稱和密碼,也可以根據需要包含其他屬性。通過獲取Secure Store Service儲存的憑據,可以用於訪問其他外部應用。根據微軟官方的描述,在SharePoint應用Secure Store Service的解決方案包括:
- Excel Services
-
Visio Services 可使用安全儲存提供對已釋出的資料連線圖表中的外部資料來源的訪問。這可以用作將使用者憑據傳遞給資料來源(此過程通常需要配置 Kerberos 委託)的替代方式。如果您需要為資料身份驗證配置無人蔘與服務帳戶,則 Visio Services 需要安全儲存。
-
PerformancePoint Services
-
PowerPivot 需要安全儲存以便對 PowerPivot 工作簿進行計劃重新整理。
-
Microsoft Business Connectivity Services 可使用安全儲存將使用者憑據對映到外部系統中的一組憑據。您可將每個使用者的憑據對映到外部系統中的唯一帳戶,也可以將一組經過身份驗證的使用者對映到單個組帳戶。Business Connectivity Services 還可以使用安全儲存來儲存用於訪問 SharePoint Online 中的本地資料來源的憑據。
-
SharePoint 執行時可使用安全儲存來儲存與 Azure 服務進行通訊所必需的憑據(如果任何使用者應用程式需要 SharePoint 執行時來設定和使用 Azure 服務)。
本文是除以上解決方案中的另外一種應用。
Secure Store Service部署在使用Secure Store Service前,我們做一些設定。
一、首先,開啟SharePoint管理中心,進入應用程式管理,點選“管理服務應用程式”
在服務應用程式列表中,找到Secure Store Service服務。該服務會在執行配置嚮導過程中預設配置,也可以點選新建手動配置(手動配置方法請看官方文件)。
二、點選進入Secure Store Service服務
1、首次配置,要生成新金鑰
2、生成新金鑰後,就可以新建目標應用程式,點選新建
3、進入新建頁面後,填寫相關內容
顯示名稱:目標應用程式顯示名稱
聯絡人email:填寫管理員email,用來使用者聯絡管理員;
目標應用程式型別:根據需要選擇型別,比如“個人”用來儲存個人憑據,“組”用來儲存組憑據(一個使用者組用一個憑據)
目標應用程式頁 URL:一般選擇預設頁,系統會有一個預設頁面(http:/<samplesite>/_layouts/SecureStoreSetCredentials.aspx?TargetAppId=<TargetApplicationID>)用來錄入使用者憑據。
4、點選下一步,進入配置欄位頁面,按照需要新增憑據需要包含的欄位(注:以後不能編輯欄位名稱和欄位型別)
在這裡,我除預設的使用者名稱和密碼外,另外新增一列EmailAddress用來儲存使用者郵箱,型別選擇一般(Genetic)。
5、繼續點選下一步
新增目標應用程式管理員,點選完成,完成建立目標應用程式
至此我們已經完成了Secure Store Service的準備工作,接下來就是實際應用的實踐。
Secure Store Service應用一、使用者憑據錄入
你可以使用系統預設的頁面(http:/<samplesite>/_layouts/SecureStoreSetCredentials.aspx?TargetAppId=<TargetApplicationID>)用於使用者憑據錄入,也可以使用自定義的頁面建立、更新使用者憑據。下面的程式碼用來更新(建立)當前使用者的特定目標應用程式憑據:
public static void SetCredentials(string appId, string[] userInfo) { List<SecureStoreCredential> creds = new List<SecureStoreCredential>(); SecureStoreCredential name = new SecureStoreCredential(toSecureString(userInfo[0]), SecureStoreCredentialType.WindowsUserName); SecureStoreCredential pwd = new SecureStoreCredential(toSecureString(userInfo[1]), SecureStoreCredentialType.WindowsPassword); SecureStoreCredential EmailAddress = new SecureStoreCredential(toSecureString(userInfo[2]), SecureStoreCredentialType.Generic); creds.Add(name); creds.Add(pwd); creds.Add(EmailAddress); SecureStoreCredentialCollection credes = new SecureStoreCredentialCollection(creds.ToArray()); SecureStoreServiceProxy proxySs = new SecureStoreServiceProxy(); SPSite site = null; SPWeb web = null; SPSecurity.RunWithElevatedPrivileges(delegate() { site = SPContext.Current.Site; web = SPContext.Current.Web; }); site.AllowUnsafeUpdates = true; web.AllowUnsafeUpdates = true; SPServiceContext context = SPServiceContext.GetContext(site); ISecureStore store = proxySs.GetSecureStore(context); store.SetCredentials(appId, credes); web.AllowUnsafeUpdates = false; site.AllowUnsafeUpdates = false; }
引數介紹:
appid:目標應用程式ID,也就是上面步驟新建的“FirstID”;
userInfo:從頁面中獲取的使用者資訊列表;
方法介紹:
1、建立欄位例項(注:例項名稱與實際目標應用程式欄位名稱沒有關聯,只要順序對就可以了,當然型別要一致)
SecureStoreCredential name = new SecureStoreCredential(toSecureString(userInfo[0]), SecureStoreCredentialType.WindowsUserName);
上面這句語句是建立一個憑據欄位,對應FirstID中的“Windows使用者名稱”,此型別包含一個2個引數的建構函式(欄位值,欄位型別);
2、建立Secure Store Service代理,獲取當前SharePoint Secure Store Service上下文環境
3、為site,web提升許可權
SPSecurity.RunWithElevatedPrivileges(delegate()
{
site = SPContext.Current.Site;
web = SPContext.Current.Web;
});
4、使用ISecureStore的SetCredentials方法更新(建立)使用者憑據。
5、最後,會注意到有一個toSecureString方法,這個方法是對字串進行安全編碼,程式碼是:
View Codepublic static System.Security.SecureString toSecureString(string s) { System.Security.SecureString secureString = new System.Security.SecureString(); foreach (Char character in s) { secureString.AppendChar(character); } return secureString; }
利用上面的程式碼,就可以為使用者配置目標應用程式的憑據。
二、根據當前使用者獲取該使用者憑據資訊
使用上面的方法將使用者憑據錄入後,下一步就是利用Secure Store Service獲取使用者憑據。
在使用EMSManagedAPI操作Exchange郵箱所在的部落格中,有一個步驟是需要使用者的賬號和密碼。另外,上面在建立目標應用程式的過程中,多加了一列EmailAddress,這樣我們就可以用EWS Managed API中AutodiscoverUrl方法,而不需要知道具體的郵件伺服器服務地址,程式碼就可以改為:
View Code
原始碼: service.Credentials = new WebCredentials(creds); service.Url = new Uri("https://伺服器地址/ews/exchange.asmx"); service.PreAuthenticate = true;
View Code
修改後: service.Credentials = new WebCredentials(creds); service.AutodiscoverUrl(EmailAddress); service.PreAuthenticate = true;
上面是對之前應用的一點優化,如果有興趣,可以去看之前的部落格。接下來是如何取得使用者憑證的例項。
Secure Store Service不需要指定使用者,會直接根據當前上下文獲得當前登陸使用者,下面是獲取使用者資訊列表的方法:
public List<string> GetUserCredentialCollection(string appId, SPServiceContext CurrentContext)//appid is the SSS' ID { List<string> credentialList = new List<string>(); SecureStoreProvider prov = new SecureStoreProvider(); SPServiceContext context = CurrentContext; prov.Context = context; //當前上下文資訊,以便從上下文中找到當前登陸使用者 try { SecureStoreCredentialCollection cc = prov.GetCredentials(appId); for (int i = 0; i < cc.Count; i++) { ISecureStoreCredential c = cc[i]; IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(c.Credential); string sDecrypString = System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr); credentialList.Add(sDecrypString); } } catch { } return credentialList; }
其實最重要的是for迴圈中的方法,根據目標應用程式ID,獲取使用者憑據集合,遍歷使用者憑據欄位並存放到List中,之後就可以根據個人需求來利用這些資訊。
到這裡Secure Store Service的應用基本就結束了,總體來說Secure Store Service有利有弊,對於安全性要求很高的使用者來說,可能並不是一個最佳的選擇。但Secure Store Service得靈活性較好,可以儲存使用者的多個應用程式憑據,對於多個系統整合有很好的相容性。有興趣的朋友,可以一起討論,這篇部落格就先寫到這裡了。