Un esempio di come recuperare via codice dal Servizio di Secure Store di SharePoint 2010 le credenziali associate all'utente corrente di una specifica application (ApplicationID).

C#

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.Office.SecureStoreService.Server; //Microsoft.Office.SecureStoreService.dll
using Microsoft.BusinessData.Infrastructure.SecureStore; //Microsoft.BusinessData.dll
using System.Runtime.InteropServices;

namespace ConsoleApplicationSSS
{
  class Program
  {
    static void Main(string[] args)
    {
      string url = "http://intranet.contoso.com";
      string applicationID = "SgartSSS_Individual";

      try
      {
        Dictionary<string, string> cred = GetCredential(url, applicationID);
        Console.WriteLine(string.Format("ApplicationID: {0}", applicationID));
        foreach (string key in cred.Keys)
        {
          string val = cred[key];
          Console.WriteLine(string.Format("Name: {0}, String: {1}", key, val));
        }
      }
      catch (SecureStoreCredentialsNotFoundException ex)
      {
        Console.WriteLine(ex.Message);
      }
    }


    public static Dictionary<string, string> GetCredential(string url, string applicationID)
    {
      Dictionary<string, string> dic = new Dictionary<string, string>();
      SecureStoreProvider secureStore = new SecureStoreProvider();
      using (SPSite site = new SPSite(url))
      {
        //secureStore.Context = SPServiceContext.Current
        secureStore.Context = SPServiceContext.GetContext(site);
        using (SecureStoreCredentialCollection credentials = secureStore.GetCredentials(applicationID))
        {
          var fields = secureStore.GetTargetApplicationFields(applicationID);
          for (var i = 0; i < fields.Count; i++)
          {
            var field = fields[i];
            var credential = credentials[i];
            string name = field.Name;
            string str = GetSecureString(credential.Credential);

            dic.Add(field.Name, str);
          }
        }
      }
      return dic;
    }


    public static string GetSecureString(System.Security.SecureString secureString)
    {
      var ptr = Marshal.SecureStringToBSTR(secureString);

      try
      {
        return Marshal.PtrToStringBSTR(ptr);
      }
      finally
      {
        Marshal.FreeBSTR(ptr);
      }
    }
  }
}

questa console eseguita con l'utente CONTOSO\Administrator da un risultato simile al seguente:

Text

ApplicationID: SgartSSS_Individual
Name: Windows User Name, String: contoso\admin
Name: Windows Password, String: pwd1
questo perché nel Secure Store Service ho associato nell'applicazione SgartSSS_Individual, all'utente CONTOSO\Administrator un mapping verso l'utente CONTOSO\admin con password pwd1.

In questo caso l'esempio è una console application. Nel caso si trattasse di una web part o una pagina nel constesto di SharePoint, non serve costruire un oggetto SPSite per ottenere il contesto del servizio ( SPServiceContext.GetContext(site) ). E' sufficiente usare SPServiceContext.Current .
La dll Microsoft.Office.SecureStoreService.dll si trova in C:\Windows\assembly\GAC_MSIL\Microsoft.Office.SecureStoreService\14.0.0.0__71e9bce111e9429c\Microsoft.Office.SecureStoreService.dll
Tags:
SharePoint497 SharePoint 2010224
Potrebbe interessarti anche: