OIOSAML / SAML2属性/属性值定义类型?

时间:2014-10-02 11:15:00

标签: c# .net xml saml-2.0

我尝试使用OIOSAML.net创建SAML2票证。

我已经获得了95%的结构正确,但是在创建xml时我遇到了以下问题。

结果:

<saml2:Attribute name="urn:x:names:federation:attributeName:systemversion" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
            <saml2:AttributeValue>1.0</saml2:AttributeValue>
</saml2:Attribute>

预期:

<saml2:Attribute Name="urn:x:names:federation:attributeName:systemversion" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
            <saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">1.0</saml2:AttributeValue>
</saml2:Attribute>

我在AttributeValue上缺少类型定义。从对象定义中我可以读出以下内容。

/// <summary>
/// Gets or sets the attribute value.
/// Contains a value of the attribute. If an attribute contains more than one discrete value, it is
/// RECOMMENDED that each value appear in its own &lt;AttributeValue&gt; element. If more than
/// one &lt;AttributeValue&gt; element is supplied for an attribute, and any of the elements have a
/// datatype assigned through xsi:type, then all of the &lt;AttributeValue&gt; elements must have
/// the identical datatype assigned.
/// </summary>
/// <value>The attribute value.</value>
[XmlElement("AttributeValue", IsNullable = true)]
public string[] AttributeValue
{
    get { return attributeValueField; }
    set { attributeValueField = value; }
}

我定义属性如下:

var attr = new SamlAttribute() {
    Name = StringConstants.SAML2_ATTRIBUTE_PREFIX + StringConstants.ATTRIBUTE_INFO_SYSTEMVERSION,
    AttributeValue = new[] {this.SystemVersion},
    NameFormat = StringConstants.ATTRIBUTE_FORMAT
};

问题 如何让<AttributeValue>定义xsi:type="xs:string"

1 个答案:

答案 0 :(得分:0)

由于SP没有处理来自XSAnyImpl的解析,我不得不制作一个自定义序列化对象。

为了实现这一点,我必须在SamlAttribute中添加一些适合我特定情况的东西。我确定应该有更好的解决方案,但这适用于我的情况。

首先忽略序列化中的默认值

[XmlIgnore]
[XmlElement("AttributeValue", IsNullable = true)]
public string[] AttributeValue
{
    get { return attributeValueField; }
    set { attributeValueField = value; }
}

添加新的自定义对象

[XmlElement("AttributeValue", IsNullable = true)]
public ExtendedAttributeValue[] Values { get; set; }


public class ExtendedAttributeValue {
        [XmlAttribute("type", DataType = "string", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }

        [XmlText]
        public string Value { get; set; }
}

强制类型为xs:string

new SamlAttribute() {
                    Name = StringConstants.SAML2_ATTRIBUTE_PREFIX + StringConstants.ATTRIBUTE_INFO_SYSTEMVERSION,
                    Values = new [] {new SamlAttribute.ExtendedAttributeValue(){Type = "xs:string",Value = this.SystemVersion}},
                    NameFormat = StringConstants.ATTRIBUTE_FORMAT
};

<强>结果

<saml2:Attribute Name="urn:x:names:federation:attributeName:systemversion" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri">
        <saml2:AttributeValue xsi:type="xs:string">1.0</saml2:AttributeValue>
</saml2:Attribute>

xs&amp; xsi在根节点中定义

<saml2:Assertion xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" Version="2.0" xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">