Code Snippet: Get User Credentials Using the Default Secure Store Provider
It demonstrates how to obtain the creden information of the current user. It is mentioned that when GetCredentials fails to obtain the value, the SecureStoreServiceException will be thrown. This exception is quite powerful, not only in ULS, event 7493 also appears in Windows logs;
using (SecureStoreCredentialCollection creds = provider.GetCredentials(appId)){// Secure Store Service will not return null. It may throw a SecureStoreServiceException,// but this may not be true for other providers.
================================================================================
The Microsoft Secure Store Service application Secure Store Service cannot retrieve creden. The returned error is "The creden。 of the current user cannot be found in the target application" eam_79. Set creden for the current user .". For more information, see Microsoft SharePoint product and technical software development kit (SDK ).
========================================================== ======================================
Therefore, it is important to avoid changing requests without creden. The solution is to directly query the SSS database table [SSSCredentials] and match the ApplicationID with the [IdentityClaimValueHash] field, if the returned value is greater than 0, the user has the Application credential in SSS. The IdentityClaimValueHash field is encrypted by Hash256;
The following is the key code:
1. Obtain the user's IdentityClaimValueHash
2. Get the database ConnecetionString of SSS
/// <Summary> /// obtain the Single Sign-On database connection string /// </summary> /// <returns> </returns> public static string GetSSSDataBaseStr () {string dbconstr = ""; bool islocalservice = false; foreach (SPServiceApplicationProxy pro in SPContext. current. site. webApplication. serviceApplicationProxyGroup. defaultProxies) {string s = pro. getType (). toString (); if (s = "Microsoft. office. secureStoreService. server. secureStoreService ApplicationProxy ") {SPIisWebServiceApplicationProxy iispro = (SPIisWebServiceApplicationProxy) pro; Uri endpoint = iispro. serviceEndpointUri; SecureStoreService svc = SPFarm. local. services. getValue <SecureStoreService> (); foreach (SecureStoreServiceApplication app in svc. applications) {// SecureStoreServiceDatabase db = app. getPrivateProperty <SecureStoreServiceDatabase> ("Database"); string svid1 = ap P. Id. ToString (); if (! Islocalservice) {// ServiceEndpointUri {urn: schemas-microsoft-com: sharepoint: service: Region # authority = urn: uuid: Region & authority = https: // irene2010rtm: 32844/Topology/topology. svc} islocalservice = endpoint. absolutePath. toLower (). endsWith (svid1.ToLower (). replace ("-", ""); if (islocalservice) {Type objectType = app. getType (); BindingFlags flag = BindingFlags. instance | BindingFlags. nonPublic; PropertyInfo WS = objectType. getProperty ("Database", flag); SecureStoreServiceDatabase db3 = (SecureStoreServiceDatabase) WS. getValue (app, null); // writer. writeLine (db3.Name + ":" + db3.DatabaseConnectionString); dbconstr = db3.DatabaseConnectionString; break ;}}} return dbconstr ;}
The following code is invalid:
1: SPFarm farm = SPFarm.Local;
2: if (null == farm)
3: {
4: throw new InvalidProgramException("SP Farm Local Not Found");
5: }
6: SecureStoreService service = farm.Services.GetValue<SecureStoreService>();
7: if (null == service)
8: {
9: throw new InvalidProgramException("Proxy Creattion Failed Error");
10: }
11: SPServiceApplication application = service.Applications.ToList().SingleOrDefault();
12: if (application is SecureStoreServiceApplication)
13: {
14: SecureStoreServiceApplication secureStoreServiceApplication = application as SecureStoreServiceApplication;
15: string strconn=((Microsoft.SharePoint.Administration.SPDatabase)(secureStoreServiceApplication.Database)).ConnectionString;
16:
17: Console.WriteLine(strconn);
18: }
3. query statement ()
Select a. ApplicationName, B. IdentityClaimValueHash
From SSSApplication a, SSSCredentials B where a. ApplicationId = B. ApplicationId and a. ApplicationName = '"+ appName. Trim () +"' and B. IdentityClaimValueHash = @ binaryValue