【问题标题】:any sample for creating an instance of SamlSecurityToken?任何用于创建 SamlSecurityToken 实例的示例?
【发布时间】:2023-04-04 13:36:01
【问题描述】:

我不确定这是不是一个好问题,但是:

我正在尝试在 Internet 上找到任何用于创建 System.IdentityModel.Tokens.SamlSecurityToken 和 System.IdentityModel.Tokens.SamlAssertion 实例的示例,但我找不到...任何帮助?

【问题讨论】:

  • Acully SamlSecurityToken 从版本 3 开始存在,我们的项目是 .Net 版本 4 但是我正在寻找任何示例

标签: security wif saml


【解决方案1】:

知道了! 来源(我写问题时服务器没有响应): 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); 
  } 
} 

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多