Beispiel #1
0
        /// <summary>
        /// Get Credential from SSS
        /// </summary>
        /// <param name="appId">Application Id</param>
        /// <param name="adminSiteUrl">Admin Site Url</param>
        /// <returns>Credential as Dictionary string and string</returns>
        public static Dictionary <string, string> GetCredentialsFromSSS(string appId, string adminSiteUrl)
        {
            var result = new Dictionary <string, string>();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    var siteAdmin = new SPSite(adminSiteUrl);

                    // Get the default Secure Store Service provider.
                    var provider = SecureStoreProviderFactory.Create();
                    if (provider == null)
                    {
                        throw new InvalidOperationException("Unable to get an ISecureStoreProvider");
                    }

                    var providerContext = provider as ISecureStoreServiceContext;
                    if (providerContext == null)
                    {
                        return;
                    }

                    providerContext.Context = SPServiceContext.GetContext(siteAdmin);

                    var secureStoreProvider = new SecureStoreProvider {
                        Context = providerContext.Context
                    };

                    // Create the variables to hold the credentials.
                    using (var creds = provider.GetCredentials(appId))
                    {
                        if (creds == null)
                        {
                            return;
                        }

                        var fields = secureStoreProvider.GetTargetApplicationFields(appId);
                        if (fields.Count <= 0)
                        {
                            return;
                        }

                        for (var i = 0; i < fields.Count; i++)
                        {
                            var field               = fields[i];
                            var credential          = creds[i];
                            var decryptedCredential = GetStringFromSecureString(credential.Credential);
                            result.Add(field.Name, decryptedCredential);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                ULSLogging.LogError(ex);
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Get credentials from Secure Store Service
        /// </summary>
        /// <param name="targetAppId">Target Application ID for the Secure Store</param>
        /// <param name="site"></param>
        /// <returns>Object of NetworkCredential class. This class provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.</returns>
        public static SecureStoreCredentials GetCredentials(string targetAppId, SPSite site)
        {
            // Get the default Secure Store Service provider.
            ISecureStoreProvider provider = SecureStoreProviderFactory.Create();

            if (provider == null)
            {
                throw new Exception("Unable to get an ISecureStoreProvider.");
            }

            ISecureStoreServiceContext providerContext = provider as ISecureStoreServiceContext;

            if (providerContext != null)
            {
                providerContext.Context = SPServiceContext.GetContext(site);
            }

            var credentials = new SecureStoreCredentials();

            using (SecureStoreCredentialCollection credentialCollection = provider.GetCredentials(targetAppId))
            {
                foreach (ISecureStoreCredential credential in credentialCollection)
                {
                    switch (credential.CredentialType)
                    {
                    case SecureStoreCredentialType.UserName:
                        credentials.UserName = GetStringFromSecureString(credential.Credential);
                        break;

                    case SecureStoreCredentialType.Password:
                        credentials.Password = credential.Credential;
                        break;

                    case SecureStoreCredentialType.WindowsUserName:
                        credentials.WindowsUserName = GetStringFromSecureString(credential.Credential);
                        break;

                    case SecureStoreCredentialType.WindowsPassword:
                        credentials.WindowsPassword = credential.Credential;
                        break;

                    case SecureStoreCredentialType.Certificate:
                        credentials.Certificate = GetStringFromSecureString(credential.Credential);
                        break;

                    case SecureStoreCredentialType.CertificatePassword:
                        credentials.CertificatePassword = credential.Credential;
                        break;
                    }
                }
            }
            return(credentials);
        }
Beispiel #3
0
        public static Dictionary <string, string> GetCredentialsFromSecureApp(string applicationId)
        {
            var credentialMap = new Dictionary <string, string>();

            // Get the default Secure Store Service provider.
            ISecureStoreProvider provider = SecureStoreProviderFactory.Create();

            if (provider == null)
            {
                throw new InvalidOperationException("Unable to get an ISecureStoreProvider");
            }

            var providerContext = provider as ISecureStoreServiceContext;

            if (providerContext != null)
            {
                providerContext.Context = SPServiceContext.GetContext(GetCentralAdminSite());
            }

            var secureStoreProvider = new SecureStoreProvider
            {
                Context = SPServiceContext.GetContext(GetCentralAdminSite())
            };

            using (var credentials = secureStoreProvider.GetCredentials(applicationId))
            {
                var fields = secureStoreProvider.GetTargetApplicationFields(applicationId);
                for (int i = 0; i < fields.Count; i++)
                {
                    var field      = fields[i];
                    var credential = credentials[i];

                    var decryptedCredential = GetStringFromSecureString(credential.Credential);

                    credentialMap.Add(field.Name, decryptedCredential);
                }
            }

            return(credentialMap);
        }