使用包含特殊字符的密码通过 SAML 请求 STS 令牌

Requesting STS Token via SAML with passwords containing special characters

编辑:我注意到错误不仅出现在五个 XML 保留字符上,还出现在其他特殊字符上。

我用于获取 STS 令牌的 SAML 请求有问题。

SAML 请求在 XML 中写入。 如果随请求发送的密码包含 XML 保留的字符,则登录失败。我试图转义字符 (& = &) 或使用 CDATA

(lBuilder.Append('<o:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"><![CDATA[').Append(lPassword).Append(']]></o:Password>');)

但是显然密码没有被正确识别并且登录仍然失败。 如果我使用 CDATA,即使使用有效密码登录也会失败。请求通过 post 命令发送到 https://login.microsoftonline.com/RST2.srf (cDefaultSTS)。

有没有办法在不冲突的情况下正确转义密码中的特殊字符? 代码以 Delphi 编写,我使用以下客户端设置发送 post 命令(lSAMLRequestStream 包含 XML 格式的请求)。

lConnection.Client.Accept := '*/*';
lConnection.Client.AcceptLanguage := 'en-US';
lConnection.Client.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)';
lConnection.Client.ContentType := 'application/soap+xml';
lConnection.Client.HandleRedirects := True;
lConnection.Client.AcceptEncoding := '';
lSTSResponse := lConnection.Post(cDefaultSTS, lSAMLRequestStream);

我在 C# 中使用 Microsoft.SharePoint 库尝试了相同的操作,但如果密码包含保留字符之一,身份验证仍然失败。转义在这里也不起作用。

using (ClientContext clientContext = new ClientContext("https:mysite@sharepoint.com"))
{
  var passWord = new SecureString();
  string passwrd = "Password&";

  string encodedXml = HttpUtility.HtmlEncode(passwrd);

  foreach (char c in encodedXml.ToCharArray()) passWord.AppendChar(c);

  clientContext.Credentials = new SharePointOnlineCredentials("username@SharePoint.com",passWord);

   Web web = clientContext.Web;
   Console.WriteLine(clientContext.Site);
   clientContext.Load(web);

   clientContext.ExecuteQuery();

   Console.WriteLine(web.Title);
   Console.ReadLine();
}

通过 CDATA 标签转义实际上是解决方案并且有效。我刚刚使用的测试帐户有一个额外的问题,它看起来仍然因为密码而失败。顺便说一下,C# 代码不需要任何额外的转义。