xml:
<?xml version="1.0" encoding="utf-8" ?><EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
entityID="http://10.162.1.220:8082/bspcore">
<IDPSSODescriptor WantAuthnRequestsSigned="true"
protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> <SingleSignOnService isDefault="true" index="0"
Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
Location="http://10.162.1.220:8082/bspcore/SAML2/POST/SSO" /> <SingleSignOnServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"Location="http://10.162.1.220:8082/bspcore/SAML2/REDIRECT/SSO" /> <ArtifactResolutionServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
Location="http://10.162.1.220:8082/bspcore/SAML2/Artifact" /> <SingleLogoutServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:SOAP"
Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/SOAP" /> <SingleLogoutServiceBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/REDIRECT"
ResponseLocation="http://10.162.1.220:8082/bspcore/SAML2/SLO/Response" /> <NameIDFormat> urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName </NameIDFormat> <NameIDFormat> urn:oasis:names:tc:SAML:2.0:nameid-format:persistent </NameIDFormat> <NameIDFormat> urn:oasis:names:tc:SAML:2.0:nameid-format:transient </NameIDFormat> </IDPSSODescriptor></EntityDescriptor>
代码:
XElement idpdoc = XElement.Load(new StreamReader("E:\\IDP.xml"));
var test = idpdoc.Descendants("NameIDFormat");
var NameIdFormats = (from ni in idpdoc.Descendants() where ni.Name=="NameIDFormat" select ni.Value).ToList<String>();
结果:
test.Count()=0;
NameIdFormats .Count()=0;
你的XML好像格式不太正确,建议你格式化下,我格式化后的XML:
<?xml version="1.0" encoding="utf-8" ?>
<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
entityID="http://10.162.1.220:8082/bspcore">
<IDPSSODescriptor WantAuthnRequestsSigned="true"
protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<SingleSignOnService isDefault="true" index="0"
Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
Location="http://10.162.1.220:8082/bspcore/SAML2/POST/SSO" />
<SingleSignOnServiceBinding xmlns="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
Location="http://10.162.1.220:8082/bspcore/SAML2/REDIRECT/SSO" />
<ArtifactResolutionServiceBinding Location="http://10.162.1.220:8082/bspcore/SAML2/Artifact" />
<SingleLogoutServiceBinding Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/SOAP" />
<SingleLogoutServiceBinding xmlns="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
Location="http://10.162.1.220:8082/bspcore/SAML2/SLO/REDIRECT"
ResponseLocation="http://10.162.1.220:8082/bspcore/SAML2/SLO/Response" />
<NameIDFormat> urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName </NameIDFormat>
<NameIDFormat> urn:oasis:names:tc:SAML:2.0:nameid-format:persistent </NameIDFormat>
<NameIDFormat> urn:oasis:names:tc:SAML:2.0:nameid-format:transient </NameIDFormat>
</IDPSSODescriptor>
</EntityDescriptor>
至于为什么会出现这样的问题,你可以将下面代码执行并打印就可以发现了:
var NameIdFormats = from ni in idpdoc.Descendants()
select ni.Name;
打印出来的代码是,包含了具体的命名空间:
{urn:oasis:names:tc:SAML:2.0:metadata}IDPSSODescriptor
{urn:oasis:names:tc:SAML:2.0:metadata}SingleSignOnService
{urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect}SingleSignOnServiceBinding
{urn:oasis:names:tc:SAML:2.0:metadata}ArtifactResolutionServiceBinding
{urn:oasis:names:tc:SAML:2.0:metadata}SingleLogoutServiceBinding
{urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect}SingleLogoutServiceBinding
{urn:oasis:names:tc:SAML:2.0:metadata}NameIDFormat
{urn:oasis:names:tc:SAML:2.0:metadata}NameIDFormat
{urn:oasis:names:tc:SAML:2.0:metadata}NameIDFormat
所以你传NameIDFormat是匹配不到的。
你可以试下这个方法:
var NameIdFormats = from ni in idpdoc.Elements()
let ni2 = ni.Elements()
from ni3 in ni2
where ni3.Name == ni3.GetDefaultNamespace() + "NameIDFormat"
select ni3.Value;