示例#1
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()));
            }
        }
        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;
 }
示例#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);
        }
    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));
    }
示例#6
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);
        }