Path: redb-route/redb.Route.Soap/SoapSecurity.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Soap/SoapSecurity.cs
----
using System.Xml.Linq;
namespace redb.Route.Soap;
///
/// WS-Security helpers: UsernameToken (pure XML, common and interoperable). XML-Signature and
/// XML-Encryption of the envelope (BinarySecurityToken / EncryptedData, full WSS policy) are a
/// dedicated effort validated against a real stack, using System.Security.Cryptography.Xml.
///
internal static class SoapSecurity
{
public const string WsseNs = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
public const string PasswordTextType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
/// Builds a <wsse:Security> header carrying a UsernameToken (as an XML string).
public static string BuildUsernameTokenHeader(string username, string? password)
{
XNamespace wsse = WsseNs;
var token = new XElement(wsse + "UsernameToken",
new XElement(wsse + "Username", username));
if (password is not null)
token.Add(new XElement(wsse + "Password", new XAttribute("Type", PasswordTextType), password));
var security = new XElement(wsse + "Security",
new XAttribute(XNamespace.Xmlns + "wsse", WsseNs),
token);
return security.ToString(SaveOptions.DisableFormatting);
}
/// Extracts (username, password) from a <wsse:Security> element, or null if absent.
public static (string Username, string? Password)? ReadUsernameToken(XElement security)
{
XNamespace wsse = WsseNs;
var token = security.Element(wsse + "UsernameToken");
var user = token?.Element(wsse + "Username")?.Value;
if (string.IsNullOrEmpty(user)) return null;
var pass = token!.Element(wsse + "Password")?.Value;
return (user, pass);
}
/// Whether an envelope header element is the <wsse:Security> element.
public static bool IsSecurityHeader(XElement element) =>
element.Name.LocalName == "Security" && element.Name.NamespaceName == WsseNs;
}