示例#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);
        }
    private static void PopulateCredentialsMap(SecureStoreProvider secureStoreProvider, SecureStoreCredentialCollection credentials, string applicationId, Dictionary <string, string> credentialMap)
    {
        var fields = secureStoreProvider.GetTargetApplicationFields(applicationId);

        for (var i = 0; i < fields.Count; i++)
        {
            var field               = fields[i];
            var credential          = credentials[i];
            var decryptedCredential = ExtractString(credential.Credential);

            credentialMap.Add(field.Name, decryptedCredential);
        }
    }
示例#3
0
        public IDictionary <string, object> Read(object key, params string[] columns)
        {
            var providerFields = _provider.GetTargetApplicationFields(_app.ApplicationId)
                                 .OfType <TargetApplicationField>();

            using (var data = _provider.GetCredentials(_app.ApplicationId)) {
                var values = providerFields.Select((x, i) => new { Field = x, Value = data[i].Credential })
                             .ToLookup(x => x.Field.Name, x => x.Field.IsMasked ? (object)x.Value : ConvertToString(x.Value));

                return(columns.Length == 0
                    ? values.ToDictionary(x => x.Key, x => x.FirstOrDefault())
                    : columns.ToDictionary(x => x, x => values[x].FirstOrDefault()));
            }
        }
示例#4
0
        public static Dictionary <string, string> GetCredentials(string applicationID)
        {
            var serviceContext      = SPServiceContext.Current;
            var secureStoreProvider = new SecureStoreProvider {
                Context = serviceContext
            };
            var credentialMap = new Dictionary <string, string>();

            using (var credentials = secureStoreProvider.GetCredentials(applicationID))
            {
                var fields = secureStoreProvider.GetTargetApplicationFields(applicationID);
                for (var i = 0; i < fields.Count; i++)
                {
                    var field               = fields[i];
                    var credential          = credentials[i];
                    var decryptedCredential = ToClrString(credential.Credential);
                    credentialMap.Add(field.Name, decryptedCredential);
                }
            }
            return(credentialMap);
        }
示例#5
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);
        }