English 中文(简体)
• 妥善整理任何习俗?
原标题:Good way to serialize any custom WCF SecurityToken?

这使我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? 看来,它本应是简单的,但不是......。

最佳回答

自2006年以来 我没有发现这样做的其他好办法,我最后做的是使我的习惯安全图也实施<条码>六舍五入>,然后将我的序列仪的定义改为:

public class SecurityTokenSerializer<T> : WSSecurityTokenSerializer
    where T : SecurityToken, IXmlSerializable

我的习惯安全被定义为:

public class MySecurityToken : SecurityToken, IXmlSerializable

然后,序列器可将序列化权下放给照相担保。 这可能是最清洁的解决办法(我说这违反了单一责任主),但它似乎行之有效,让我不必为每一种习俗写一个序列器。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签