0

我一直在使用 Secure Store 时遇到问题。当我部署或执行 IISRESET 时,我的 Secure Store 工作了几个小时,但随后 Secure Store 死机,我得到以下错误:

System.ServiceModel.EndpointNotFoundException: Secure Store Service did not 
performed the operation. 


System.ServiceModel.EndpointNotFoundException: Secure Store Service did not 
performed the operation. at    Microsoft.Office.SecureStoreService.Server.
SecureStoreServiceApplicationProxy.Execute[T](String operationName, Boolean 
validateCanary, ExecuteDelegate`1 operation) at icrosoft.Office.SecureStoreService.
Server.SecureStoreServiceApplicationProxy.GetCredentials(Guid rawPartitionId, 
String applicationId) at 
Microsoft.Office.SecureStoreService.Server.SecureStoreProvider.
GetCredentials(String appId)

我的选择已经不多了,这就是我在这里问的原因。

我已经研究过 Nulling 对象......从我在这里读到的内容:http: //davidlozzi.com/tag/secure-store-service/

这是我检索我的安全存储凭据的代码..

   public static Dictionary<string, string> GetCredentials(string applicationID)
    {
        var credentialMap = new Dictionary<string, string>();
        var serviceContext = SPServiceContext.Current;
        var secureStoreProvider = new SecureStoreProvider { Context = serviceContext };

        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);
                credentials[i].Dispose();
            }

        }
        serviceContext = null;
        secureStoreProvider = null;
        return credentialMap;
    }

..但它似乎也不起作用。

任何建议都会非常有帮助。谢谢!

4

1 回答 1

0

请参阅下面的代码 - 我们在后端计时器作业和前端用例中使用它几个月都没有问题。

唯一的区别似乎是SPServiceContext每次请求凭证时的重新创建。尝试这样做,而不是重用当前上下文。

public class ExternalCredentialsRepository
{
    private readonly string webUrl;

    public ExternalCredentialsRepository(string webUrl)
    {
        this.webUrl = webUrl;
    }

    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);
    }

    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);
        }
    }

    private static string ExtractString(SecureString secureString)
    {
        var ptr = Marshal.SecureStringToBSTR(secureString);

        try
        {
            return Marshal.PtrToStringBSTR(ptr);
        }
        finally
        {
            Marshal.FreeBSTR(ptr);
        }
    }
}
于 2013-08-14T18:06:06.860 回答