如何存储/反序列化/序列化GenericXmlSecurityToken?

问题描述 投票:2回答:2

我已经在c#中构建了一个winforms应用程序来从ADFS(windowsmixed)请求一个身份验证令牌,它工作正常,我发出了一个令牌。我正在使用WIF / ThinkTecture IdentityModel。

什么是在本地缓存/保存令牌然后重新加载令牌的最佳方法(这样我就可以验证)?

我以GenericXmlSecurityToken对象的形式发出了一个令牌。什么是将其保存/缓存到文件然后将其重新加载/序列化为新的GenericXmlSecurityToken对象的最佳方法?

我可以将令牌转换为字符串并保存:

string strToken = ((GenericXmlSecurityToken)genericToken).TokenXml.OuterXml;

但是我不知道如何将这个字符串重新序列化回GenericXmlSecurityToken,或者即使这是最好的方法(我感谢将令牌保存到磁盘时需要额外的安全性,这是未来的任务)。

c# token wif claims-based-identity adfs
2个回答
0
投票

如果您只是想要反序列化的令牌来初始化WCF(我认为该方法的名称类似于创建WithIssuedToken),您可以使用constructor of GenericXmlSecurityToken。仅使用XmlElement tokenXml参数并将其他参数保留为null(或DateTime参数的某些默认值)

var xmlToken = new GenericXmlSecurityToken(
        tokenXmlElement,
        null,
        DateTime.Now,
        DateTime.Now.AddHours(1),
        null,
        null,
        null);

我应该注意,实例将具有属性(如effectiveTime和expirationTime),这些属性不会与现在被deserialed SecurityToken的实际值相对应(因为您在构造函数中手动设置它们)。但是WCF将使用令牌中的真实信息,因此它将以这种方式工作。


0
投票

使用System.IdentityModel可以将GenericXmlSecurityToken序列化为Xml。以下是Shawn Cicoria的一篇文章,给出了如何做到这一点的例子:http://geekswithblogs.net/cicorias/archive/2012/03/22/getting-a-securitytoken-from-a-requestsecuritytokenresponse-in-wif.aspx

给定序列化令牌,您将不得不设计一种方法来保存它。

注意:如果尚未添加对System.IdentityModel和System.IdentityModel.Services的引用,请添加对它的引用。

© www.soinside.com 2019 - 2024. All rights reserved.