2012-12-05 66 views
0

我不知道,如果它的一個很好的問題或沒有,但:任何用於創建SamlSecurityToken實例的示例?

我試圖找到任何樣品在創建System.IdentityModel.Tokens.SamlSecurityToken和System.IdentityModel.Tokens.SamlAssertion的一個實例互聯網,但我沒有找到...任何幫助?

+0

這是WIF 4.5嗎? – nzpcmad

+0

從版本3開始存在SamlSecurityToken,我們的項目是.Net版本4,但是我正在尋找任何示例 –

回答

2

Got it! 源(服務器並沒有在我寫這個問題的時間響應): http://developers.de/blogs/damir_dobric/archive/2007/02/22/Creating-of-SAML-token.aspx

private static void Main(string[] args) 
{ 
    SamlAssertion assertion = createSamlAssertion(); 
    SamlSecurityToken samlToken = new SamlSecurityToken(assertion); 
} 

/// <summary> 
/// Creates some Test SAML assertion 
/// </summary> 
/// <returns></returns> 
private static SamlAssertion createSamlAssertion() 
{ 
    // Here we create some SAML assertion with ID and Issuer name. 
    SamlAssertion assertion = new SamlAssertion(); 
    assertion.AssertionId = "DaenetSamlTest"; 
    assertion.Issuer = "damir"; 

    // 
    // Create some SAML subject. 
    SamlSubject samlSubject = new SamlSubject(); 
    samlSubject.Name = "My Subject"; 

    // 
    // Create one SAML attribute with few values. 
    SamlAttribute attr = new SamlAttribute(); 
    attr.Namespace = http://daenet.eu/saml; 
    attr.AttributeValues.Add("Some Value 1"); 
    attr.AttributeValues.Add("Some Value 2"); 

    attr.Name = "My ATTR Value"; 

    // 
    // Now create the SAML statement containing one attribute and one subject. 
    SamlAttributeStatement samlAttributeStatement = new SamlAttributeStatement(); 
    samlAttributeStatement.Attributes.Add(attr); 
    samlAttributeStatement.SamlSubject = samlSubject; 

    // Append the statement to the SAML assertion. 
    assertion.Statements.Add(samlAttributeStatement); 

    return assertion; 
} 

這是簽署斷言

/// <summary> 
/// Creates some signed Test SAML assertion. 
/// </summary> 
/// <returns></returns> 
private static SamlAssertion createSamlAssertion() 
{ 
    // 
    // Create certificate from file. It must contain private key! 
    X509Certificate2 cert = new X509Certificate2("filename.cert"); 

    // The private key contained in the certificate will be used to sign the 
    token. 
    X509AsymmetricSecurityKey signingKey = new X509AsymmetricSecurityKey(cert); 
    SamlAssertion assertion = createSamlAssertion(); 

    // 
    // Signing credentials are consisted 
    // of private key in the certificate (see above), 
    // the signature algorithm, security algortihm and key identifier. 
    assertion.SigningCredentials = 
    new SigningCredentials(signingKey, SecurityAlgorithms.RsaSha1Signature,  
    SecurityAlgorithms.Sha1Digest, 
    new SecurityKeyIdentifier(new X509ThumbprintKeyIdentifierClause(cert))); 

    // Finally create the SamlSecurityToken from the assertion 
    SamlSecurityToken samlToken = new SamlSecurityToken(assertion); 

    // Create a SecurityTokenSerializer that 
    // will be used to serialize the SamlSecurityToken 
    WSSecurityTokenSerializer ser = new WSSecurityTokenSerializer(); 
    using (XmlWriter xWriter = XmlWriter.Create("saml.xml")) 
    { 
    ser.WriteToken(xWriter, samlToken); 
    } 
}