2012-03-23 49 views
0

如何使用c#和SharePoint 2010 apis將證書導入到SharePoint 2010(將用於可信身份提供程序的證書,它是鏈式證書)?來自Visual Studio的SPTrustedRootAuthority(C#)

PowerShell有新的SPTrustedRootAuthority,但我不能使用PowerShell。 我使用dll上的反射,並且New-SPTrustedRootAuthority使用的是SPTrustedRootAuthorityManager類,它是內部的。

任何想法?

回答

1

這會將指定的X509證書添加到SharePoint 2010的「可信關係」存儲中。

使用

var cert = new X509Certificate2(@"C:\my_cert.cer"); 

AddTrustedRootAuthority("My Cert", cert); 

的Util

public static void AddTrustedRootAuthority(string name, X509Certificate2 certificate) 
{ 
    Type SPTrustedRootAuthorityManagerType = Type.GetType("Microsoft.SharePoint.Administration.SPTrustedRootAuthorityManager, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", true); 
    Type SPTrustedRootAuthorityType = Type.GetType("Microsoft.SharePoint.Administration.SPTrustedRootAuthority, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", true); 

    object manager = SPTrustedRootAuthorityManagerType.GetProperty("LocalOrThrow", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null); 

    object authority = Activator.CreateInstance(SPTrustedRootAuthorityType, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { name, manager, certificate }, null); 

    SPTrustedRootAuthorityType.GetMethod("Update", Type.EmptyTypes).Invoke(authority, null); 
}