Exemple #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);
        }
    }
        internal static NetworkCredential GetSecureStoreCredentials(SPSite site, string secureStoreAppId)
        {
            string _userName = null;
            string _password = null;

            NetworkCredential credentials = null;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    SecureStoreProvider ssp = new SecureStoreProvider();

                    SPServiceContext context = SPServiceContext.GetContext(site);
                    ssp.Context = context;

                    SecureStoreCredentialCollection cc =
                        ssp.GetCredentials(secureStoreAppId);


                    foreach (SecureStoreCredential c in cc)
                    {
                        if (c.CredentialType == SecureStoreCredentialType.UserName)
                        {
                            _userName = c.Credential.ToClrString();
                        }

                        if (c.CredentialType == SecureStoreCredentialType.Password)
                        {
                            _password = c.Credential.ToClrString();
                        }
                    }

                    credentials = new NetworkCredential(_userName, _password);
                });
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get credentials for application " + secureStoreAppId);
            }

            return(credentials);
        }
 /// <summary>
 /// 从SSS中获取用户账户和密码
 /// </summary>
 private void GetCurrentUserInfo()
 {
     string m_userName = string.Empty;
     string m_password = string.Empty;
     string m_appId = "ShippingID";
     SecureStoreProvider m_provider = new SecureStoreProvider();
     SPSite m_site = SPContext.Current.Site;
     SPServiceContext m_serviceContext = SPServiceContext.GetContext(m_site);
     m_provider.Context = m_serviceContext;
     try {
         SecureStoreCredentialCollection m_sscc = m_provider.GetCredentials(m_appId);
         foreach (SecureStoreCredential ssc in m_sscc)
         {
             switch (ssc.CredentialType)
             {
                 case SecureStoreCredentialType.Generic:
                     break;
                 case SecureStoreCredentialType.Key:
                     break;
                 case SecureStoreCredentialType.Password:
                     m_password = ToClrString(ssc.Credential);
                     break;
                 case SecureStoreCredentialType.Pin:
                     break;
                 case SecureStoreCredentialType.UserName:
                     m_userName = ToClrString(ssc.Credential);
                     break;
                 case SecureStoreCredentialType.WindowsPassword:
                     break;
                 case SecureStoreCredentialType.WindowsUserName:
                     break;
                 default:
                     break;
             }
         }
     }
     catch (Exception ex)
     {
         Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "alert('该用户未在SSS里维护');", true);
     }
     this.hfUserName.Value = m_userName;
     this.hfPsd.Value = m_password;
 }
Exemple #5
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);
        }
    public NetworkCredential GetCredentials(string applicationId)
    {
        var credentialMap = new Dictionary <string, string>();

        using (var site = new SPSite(webUrl))
        {
            var serviceContext      = SPServiceContext.GetContext(site);
            var secureStoreProvider = new SecureStoreProvider {
                Context = serviceContext
            };

            using (var credentials = secureStoreProvider.GetCredentials(applicationId))
                PopulateCredentialsMap(secureStoreProvider, credentials, applicationId, credentialMap);
        }

        string userName = credentialMap["Windows User Name"];
        string domain   = credentialMap["Windows Domain"];
        string password = credentialMap["Windows Password"];

        return(new NetworkCredential(userName, password, domain));
    }
        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);
        }
 public SecureStoreDataStorage(SecureStoreProvider provider)
 {
     _provider = provider;
 }
 public SecureStoreDataStorage(SecureStoreProvider provider)
 {
     _provider = provider;
 }
Exemple #10
0
 public SecureStoreContainer(TargetApplication app, SecureStoreProvider provider)
 {
     _app      = app;
     _provider = provider;
 }