2014-11-05 50 views
2

以下是使用WSTrustChannelFactory獲取tokem的示例。 From here如何將證書傳遞給WSTrust以獲取Saml令牌

var stsBinding = new WS2007HttpBinding(); 
stsBinding.Security.Mode = SecurityMode.TransportWithMessageCredential; 
stsBinding.Security.Message.EstablishSecurityContext = false; 
stsBinding.Security.Message.NegotiateServiceCredential = false; 
stsBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; 


WSTrustChannelFactory trustChannelFactory = new WSTrustChannelFactory(
    stsBinding 
    , new EndpointAddress(tokenurl) 
    ); 
trustChannelFactory.TrustVersion = System.ServiceModel.Security.TrustVersion.WSTrust13; 

X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
myStore.Open(OpenFlags.ReadOnly); 
X509Certificate2Collection coll = myStore.Certificates.Find(X509FindType.FindBySerialNumber, "MycertSerialNumber", true); 
X509Certificate2 cert = coll[0]; 
trustChannelFactory.Credentials.ClientCertificate.Certificate = cert; 

WSTrustChannel channel = (WSTrustChannel)trustChannelFactory.CreateChannel(); 

RequestSecurityToken rst = new RequestSecurityToken(RequestTypes.Issue, keyType); 
rst.AppliesTo = new EndpointAddress(realm); 
RequestSecurityTokenResponse rstr = null; 
rst.TokenType = SecurityTokenTypes.Saml; 

SecurityToken token = channel.Issue(rst, out rstr); 

現在我沒有用戶名/密碼,但提供者給了我證書.pfx文件。 如何將它傳遞給WSTrushChannelFactory?我試過使用CertificateBinding但沒有成功。

更新的代碼上面:2014年11月5日:

收到此錯誤:ID3242:安全令牌無法被驗證或授權。

回答

1

使用ClientCertificate屬性:

var stsBinding = new WS2007HttpBinding(); 
stsBinding.Security.Mode = SecurityMode.TransportWithMessageCredential; 
stsBinding.Security.Message.EstablishSecurityContext = false; 
stsBinding.Security.Message.NegotiateServiceCredential = false; 

// select the authentication mode of Client Certificate 
stsBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; 

var wifChannelFactory = new WSTrustChannelFactory(stsBinding, stsEndpoint); 
wifChannelFactory.TrustVersion = TrustVersion.WSTrust13; 

// Supply the credentials 
wifChannelFactory.Credentials.ClientCertificate.Certificate = config.Certificate; 

的PFX你可以通過certmgr.msc管理單元import to your certificate店。確保您的應用程序運行的帳戶爲has access to the private key。你可以使用x509certificate2reference it in the store

+0

米奇,你的建議讓我更進一步,比我之前,但現在得到這個錯誤:ID3242:安全令牌無法驗證或授權。 – gbs 2014-11-05 17:08:04

+0

@gbs,我假設你的意思是當你嘗試使用你收到的令牌時,你會得到這個錯誤。 ID3242通常是由於指定了錯誤的受衆uri而導致的。確保你的AppliesTo符合STS的要求,並配置RP接受的內容。另一件事情可能是STS上配置的簽名或加密證書與RP不匹配。 – Mitch 2014-11-06 00:07:24

+0

未使用但請求令牌。 STS向我發送那個錯誤。我已經將它發送給提供商,他們也在研究它。 – gbs 2014-11-06 00:28:43

0

你在這裏。

private static SecurityToken RequestSecurityToken()  
{  
    // set up the ws-trust channel factory  
    var factory = new WSTrustChannelFactory( 
     new UserNameWSTrustBinding(
      SecurityMode.TransportWithMessageCredential),  
      _idpAddress);  
    factory.TrustVersion = TrustVersion.WSTrust13;    

    var authCertificate = X509.LocalMachine.My.Thumbprint.Find(Properties.Settings.Default.RassCertificateThumbprint).FirstOrDefault(); 
    if (authCertificate == null) 
     throw new InternalException(String.Format("No atuhentication certificate found in store with thumbprint {0}.", Properties.Settings.Default.ClientCertificateThumbprint)); 

    // overenie je na zaklade certifikatu RASS 
    factory.Credentials.ClientCertificate.Certificate = authCertificate; 

    // create token request 
    var rst = new RequestSecurityToken  
    {  
     RequestType = RequestTypes.Issue, 
     KeyType = KeyTypes.Symmetric,  
     AppliesTo = new EndpointReference(_serviceAddress.AbsoluteUri)  
    }; 

    // request token and return 
    return factory.CreateChannel().Issue(rst);  
} 

順便說一句:@Mitch是正確的訪問私鑰。我只是採取了你的方法,並更換了幾行代碼。

+0

pepo,我跟隨米奇的建議,並更新我的上面的代碼,但現在我得到一個錯誤。 – gbs 2014-11-05 17:08:58

+0

你從哪裏得到這個錯誤。它是否在'SecurityToken token = channel.Issue(rst,out rstr);'或者當您嘗試使用收到的令牌時。 – pepo 2014-11-05 19:12:46

+0

是的,你指出的是同一行。 – gbs 2014-11-05 19:20:59