Monday, September 17, 2012

SharePoint 2010: Retrieving Credentials from Secure Store Service


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.SecureStoreService.Server;
using Microsoft.BusinessData.Infrastructure.SecureStore;
namespace SSSTEST
{
    class Program
  
{
        static void Main(string[] args)
        {
            //Retrieves the current users application username and password.
          
SecureStoreProvider prov = new SecureStoreProvider();
            using(SPSite site = new SPSite(http://SERVERNAME))
            {
                Console.WriteLine(site.RootWeb.CurrentUser.Name);
                SPServiceContext context = SPServiceContext.GetContext(site);
                prov.Context = context;
                string appID = "APPLICATIONID";
                try
              
{
                    SecureStoreCredentialCollection cc = prov.GetCredentials(appID);
                    foreach (SecureStoreCredential c in cc)
                    {
                        IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(c.Credential);
                        string sDecrypString = System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr);
                        Console.WriteLine(sDecrypString);
                    }
                }
                catch(Exceptionex)
                {
                    Console.WriteLine("Unable to get credentials for application "+ appID);
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
        }
    }
}

No comments:

Post a Comment