在HttpContext.Current.User(IPrincipal)中存储扩展标识

时间:2010-05-14 08:08:47

标签: c# asp.net

我创建了一个扩展ExtendedId的{​​{1}}类。 (这存储Id和名称)

在httpsodule中,我将此扩展ID存储在Current.User中,如下所示:

GenericIdentity

问题是,稍后,如何再次使用HttpContext.Current.User = new GenericPrincipal(myExtendedId, roles); 类型?

如果我试试这个:

ExtendedId

我收到了投射错误。我有一种感觉,我在这里做了一些愚蠢的铸造,但今天早上我有点模糊。

2 个答案:

答案 0 :(得分:2)

ExtendedId eId =(ExtendedId)((GenericPrincipal)HttpContext.Current.User).Identity;

ExtendedId eId =((GenericPrincipal)HttpContext.Current.User).Identity as ExtendedId;

答案 1 :(得分:1)

实际上,这是一个愚蠢的错误。在HttpContext.Current.User.Identity尚未设置为ExtendedId(GenericIdentity为标准)的情况下,对ExtendedId的强制转换失败。我添加了一个类型检查以防止这种情况。抱歉浪费了人们的时间!