SAMLRequest
. The param is an encoded block of xml that describes the SAML request. So far so good. 将 SAML 转换为查询字符串参数时出现问题。我的处理过程是:
- 生成一个 SAML 字符串
- 压缩此字符串
- 字符串用Base64 编码
- 字符串进行 UrlEncode .
SAML 请求
<samlp:AuthnRequest
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
ID="{0}"
Version="2.0"
AssertionConsumerServiceIndex="0"
AttributeConsumingServiceIndex="0">
<saml:Issuer>URN:xx-xx-xx</saml:Issuer>
<samlp:NameIDPolicy
AllowCreate="true"
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient"/>
</samlp:AuthnRequest>
代码
private string GetSAMLHttpRedirectUri(string idpUri)
{
var saml = string.Format(SAMLRequest, Guid.NewGuid());
var bytes = Encoding.UTF8.GetBytes(saml);
using (var output = new MemoryStream())
{
using (var zip = new DeflaterOutputStream(output))
{
zip.Write(bytes, 0, bytes.Length);
}
var base64 = Convert.ToBase64String(output.ToArray());
var urlEncode = HttpUtility.UrlEncode(base64);
return string.Concat(idpUri, "?SAMLRequest=", urlEncode);
}
}
我怀疑压缩有点怪。我使用SharpZipLib 的 DeflaterOutputStream
类。 据说是实施行业标准的压缩算法,也许有某些设置在这里我有错吗? The encoded output can be tested using this SAML2.0 Debugger (its a useful online conversion tool). When I decode my output using this tool it comes out as nonsense. 因此,问题是: 如何将 SAML 字符串正确转换 编码 SAMLRequest 查询参数? 谢谢 EDIT 1 The accepted answer below gives the answer to the problem. Here is final code as corrected by all subsequent comments and answers. 下面接受的答案给出了问题的答案。这里是依据下面的评论和答案,然后经更正后得出的最终代码,. Encode SAMLRequest – Working Code 编码 SAMLRequest – 可用代码
private string GenerateSAMLRequestParam()
{
var saml = string.Format(SAMLRequest, Guid.NewGuid());
var bytes = Encoding.UTF8.GetBytes(saml);
using (var output = new MemoryStream())
{
using (var zip = new DeflateStream(output, CompressionMode.Compress))
{
zip.Write(bytes, 0, bytes.Length);
}
var base64 = Convert.ToBase64String(output.ToArray());
return HttpUtility.UrlEncode(base64);
}
}
The SAMLRequest
variable contains the SAML shown at the top of this question. Decode SAMLResponse – Working Code
private string DecodeSAMLResponse(string response)
{
var utf8 = Encoding.UTF8;
var bytes = utf8.GetBytes(response);
using (var output = new MemoryStream())
{
using (new DeflateStream(output, CompressionMode.Decompress))
{
output.Write(bytes, 0, bytes.Length);
}
var base64 = utf8.GetString(output.ToArray());
return utf8.GetString(Convert.FromBase64String(base64));
}
}
6票 |
I””ve just run the following code with your example SAML:
The test variable is
this code produces a |
4票 |
The question at the top contains a “Decode SAMLResponse – Working Code” section, but that code seemed broken. After trying a few things, I discovered that it was trying to read and write to the same stream at the same time. I reworked it by separating the read and write streams and here is my solution (I am providing the request section for convenience and clarity): Encode SAML Authentication Request:
Decode SAML Authentication Response:
|