2014-01-28 46 views
0

我已經嘗試了幾種這樣做的方法,但是我還沒有成功。以編程方式添加IIS 8證書映射

我試過不成功以適應來自Microsoft的此腳本。有一個地方我找不到一個錯誤。有人能幫助我嗎?

set arguments = WScript.Arguments 
if (arguments.length < 3 or arguments.length > 4) then 
    WScript.Echo("Usage certmap.vbs <.cer file name> <userName> <password> [site]") 
    WScript.Quit(0) 
end if 

certName = arguments(0) 
user = arguments(1) 
password = arguments(2) 
site = "Default Web Site" 

if (arguments.length = 4) then 
    site = arguments(3) 
end if 

const forReading = 1 

set shell = CreateObject("WScript.Shell") 
set fso = CreateObject("Scripting.FileSystemObject") 
cer = "" 

set f = fso.OpenTextFile(certName, forReading) 
s = f.ReadLine() 

if (s <> "-----BEGIN CERTIFICATE-----") then 
    f.Close 
    shell.Run "cmd /C certutil -encode -f " + certName + " certToMap64.cer", 0, true 
    set f = fso.OpenTextFile("certToMap64.cer", forReading) 
    s = f.ReadLine() 
end if 

do while f.AtEndOfStream <> true 
    s = f.ReadLine 
    if f.AtEndOfStream <> true then 
     cer = cer + s 
    end if 
loop 

f.Close 

WScript.Echo cer 

configPath = "MACHINE/WEBROOT/APPHOST/" + site 
configSectionName = "system.webServer/security/authentication/iisClientCertificateMappingAuthentication" 

set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager") 
set iisCertMap = adminManager.GetAdminSection(configSectionName, configPath) 

iisCertMap.Properties.Item("enabled").Value = "true" 
iisCertMap.Properties.Item("oneToOneCertificateMappingsEnabled").Value = "true" 

set oneToOneMappingsElement = iisCertMap.ChildElements.Item("oneToOneMappings") 
set mapping = oneToOneMappingsElement.collection.CreateNewElement() 

mapping.Properties.Item("certificate").Value = cer 
mapping.Properties.Item("enabled").Value = "true" 
mapping.Properties.Item("userName").Value = user 
mapping.Properties.Item("password").Value = password 

oneToOneMappingsElement.Collection.AddElement(mapping) 
adminManager.CommitChanges() 

一旦代碼certmappings.vbs內複製並與相應的參數執行它示出了在與消息最後一行的錯誤:「鍵集不存在」。我想知道錯誤應該在這之前的某個地方,並且在提交完成時出現。

我使用Windows 2012 R2和IIS 8.5.9600.16384。這個腳本被放置在IIS7中工作。

任何想法?

非常感謝。

回答

0

解決!

我找到了一個解決方法,它使用了一個與IIS8一起提供的命令行工具。

appcmd.exe set config "Default Web Site" -section:system.webserver/security/authentication/iisClientCertificateMappingAuthentication /+"onetoonemappings.[userName='midominio\jgrodrigo',password='[email protected]',certificate='MIIJhzCCCG+gAwIBAgIQVOKUGessUOCW... 

它工作正常。希望對於有這樣問題的下一個傢伙有用。

乾杯!

相關問題