这使我cra笑...... I m 试图用一种源自System.Identity Model. Tokens. Security Token<>code的习俗。
public class TestSecurityToken : SecurityToken
{
public override string Id
{ get { return "123"; } }
public override ReadOnlyCollection<SecurityKey> SecurityKeys
{ get { return new ReadOnlyCollection<SecurityKey>(new List<SecurityKey>(0)); } }
public override DateTime ValidFrom
{ get { return DateTime.MinValue; } }
public override DateTime ValidTo
{get { return DateTime.MaxValue; } }
public string Property1 { get; set; }
}
I know I can write a custom serializer for it that extends WSSecurityTokenSerializer
, but I am trying to find a way to do that in a generic way, where I can make a serializer that can serialize any <T> where T : SecurityToken
这是我所尝试的,失败了:
Attempt 1: Make the SecurityToken a DataContract
[DataContract]
public class TestSecurityToken : SecurityToken
失败是因为你可以有一个数据盘,其来源是数据盘上的一个基级,而安全图就没有数据。
Attempt 2: Serialize with an XmlSerializer
public class SecurityTokenSerializer<T> : WSSecurityTokenSerializer where T : SecurityToken
{
private readonly XmlSerializer serializer;
public SecurityTokenSerializer()
{
serializer = new XmlSerializer(typeof (T));
}
protected override void WriteTokenCore(XmlWriter writer, SecurityToken token)
{
serializer.Serialize(writer, token);
}
这有误:
系统. InvalidOperation 例外: 存在一个反映类型错误 部分。 Services.Core. ServModel. Security TokenSerializer Test. Security Token . 页: 1 无效行动 例外: 为了达到XML的序号,从ICollection继承的类型必须在继承等级的所有层次上实施Add(System.Identity Model. Tokens. SecurityKey)。 System.Collections.ObjectModel.ReadOnlyCollection'1 [[System.Identity Model. Tokens. SecurityKey, System. 身份模型,第4.0.0版,文化中立,公共Key Token=b77a5c561934e089] Add(System.Identity Model. Tokens. SecurityKey)。
由于这一财产属于我的习惯<代码>。 Security Token:
public override ReadOnlyCollection<SecurityKey> SecurityKeys
{ get { return new ReadOnlyCollection<SecurityKey>(new List<SecurityKey>(0)); } }
Attempt 3: Same as above, buttries to notes the XmlSerializer toook that SecurityKeys property:
public class SecurityTokenSerializer<T> : WSSecurityTokenSerializer where T : SecurityToken
{
private readonly XmlSerializer serializer;
public SecurityTokenSerializer()
{
var xOver = new XmlAttributeOverrides();
xOver.Add(typeof(T), "SecurityKeys", new XmlAttributes { XmlIgnore = true });
serializer = new XmlSerializer(typeof (T), xOver);
}
这一错误与以往尝试相同。 它不忽视这一财产。
是否有任何其他想法,说明如何将任何安全编号为XmlReader/XmlWriter? 看来,它本应是简单的,但不是......。