これは私が狂気作っている...私は例えばSystem.IdentityModel.Tokens.SecurityToken
から派生したカスタムWCFの認証トークンを持ってしようとしている:カスタムWCF SecurityTokenをシリアル化するよい方法はありますか?
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; }
}
私はWSSecurityTokenSerializer
を拡張し、それのためのカスタム・シリアライザを書くことができます知っているが、私はここで任意の<T> where T : SecurityToken
をシリアル化することができシリアライザを作ることができ、一般的な方法でそれを行うための方法を見つけようとしていますことは、私が試した、そして失敗したものです:
試み1:あなたはセキュリティトークンではないのDataContract、ではない基本クラスから派生するのDataContractを持ってカントので、セキュリティトークンのDataContract
[DataContract]
public class TestSecurityToken : SecurityToken
が失敗してください。
試み2:
public override ReadOnlyCollection<SecurityKey> SecurityKeys
{ get { return new ReadOnlyCollection<SecurityKey>(new List<SecurityKey>(0)); } }
:原因私のカスタム
SecurityToken
でこのプロパティに
System.InvalidOperationException: There was an error reflecting type 'PartsSource.Services.Core.ServiceModel.SecurityTokenSerializerTest.TestSecurityToken'. ---> System.InvalidOperationException: To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.IdentityModel.Tokens.SecurityKey) at all levels of their inheritance hierarchy. System.Collections.ObjectModel.ReadOnlyCollection`1[[System.IdentityModel.Tokens.SecurityKey, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] does not implement Add(System.IdentityModel.Tokens.SecurityKey).
: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);
}
これはエラーで失敗してシリアライズ
試み3:上記と同じですが、そのSecurityKeysプロパティを無視するようにXmlSerializerを教えしよう:
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をに任意のセキュリティトークンをシリアライズする方法上の任意の他のアイデアを持っていますか?それは死んだシンプルだったはずだが、そうではないようだ...
MySecurityTokenにSerializableがどのように機能するかのコードの例を挙げてください。 @ CodeWithSpike – Zinov
@ Zinov。 SecurityTokensに固有の問題ではありません。 http://www.codeproject。com/Articles/43237/How-to-Implement-IXmlSerializable-正しくhttp://stackoverflow.com/questions/279534/proper-way-to-implement-ixmlserializable – granadaCoder
シリアル化コードを表示できますか?私はあなたを模倣しましたが、同じエラーが発生しました。(トークンシリアライザは 'TestSecurityToken'をシリアル化できません。カスタムタイプの場合は、カスタムシリアライザを用意する必要があります。/**/ xws.Indent = false;/**/ xws.NewLineHandling = NewLineHandling.None;/**/ /**/ SecurityTokenSerializer ser = new SecurityTokenSerializer ();/**/ using(XmlWriter xw = XmlWriter.Create(@ "C:\ temp \ myfile.xml"、 xws))/ **/ { \t ser.WriteToken(xw、myToken);/**/ } –
granadaCoder