【问题标题】:C# SAML Parsing to XML to get the value of AudienceC# SAML 解析为 XML 以获取 Audience 的值
【发布时间】:2016-04-25 18:34:06
【问题描述】:

我尝试了几种解析 SAML 的方法

       string raw = txt.Text;
        if (raw.Contains('%'))
        {
            raw = HttpUtility.UrlDecode(raw);
        }
        byte[] xmlMessageBytes = Convert.FromBase64String(raw);
        XmlDocument document = new XmlDocument { PreserveWhitespace = true };
        document.LoadXml(Encoding.UTF8.GetString(xmlMessageBytes));

但我在响应中得到随机字符。但是当我使用 Firefox 插件 SAMLTracer 时,它工作得非常好。

非常感谢任何帮助。

【问题讨论】:

  • 除非是为了好玩/学习,否则我建议您不要创建自己的 SAML2 实现。关键的安全机制很容易出错(XML 签名包装漏洞在 SAML 实现中很常见)。

标签: c# xml saml


【解决方案1】:

SAML 消息是否作为查询字符串参数传递?在这种情况下,它使用的是 Http Redirect 绑定,并且消息内容是 deflate 编码的。

作为参考,这是来自Kentor.AuthServices 库的重定向解码:

using (var compressed = new MemoryStream(payload))
{
  using (var decompressedStream = new DeflateStream(compressed, CompressionMode.Decompress, true))
  {
    using (var deCompressed = new MemoryStream())
    {
      decompressedStream.CopyTo(deCompressed);
      var xml = new XmlDocument()
      {
        PreserveWhitespace = true
      };

      xml.LoadXml(Encoding.UTF8.GetString(deCompressed.GetBuffer()));

      // Simplified, the real code does some more work.
      return xml;
    }
  }
}

【讨论】:

  • 非常感谢,有没有办法让它在 2.0 中工作,因为某些原因它在 4.0 框架中不受支持
  • 在 2.0 中遇到 CopyTo 问题
猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 2012-12-13
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2023-04-08
相关资源
最近更新 更多