我想扩展 System.Web.HttpContext.User 对象(ASP.NET/VB.NET),以便它包含除名称之外的其他字段。我知道我可以创建一个继承 System.Security.Principal.GenericPrincipal 类的对象,但是如何以可用的方式将其存储在 Current.User 对象中。即,我可以做类似的事情Current.User.UserID
。
到目前为止,为了实现这一点,我已经通过使用 | 创建了一个笨拙的解决方法。在 User.Name 属性中分隔字符串,然后拆分它们,但这有点荒谬。
有什么建议么?
谢谢!
编辑:我尝试了以下方法无济于事:
Imports System.Security.Principal
Public Class CurrentUser : Inherits GenericPrincipal
Private _totalpoints As Integer
Private _sentencecount As Integer
Private _probationuntil As DateTime
Public ReadOnly Property TotalPoints() As Integer
Get
Return _totalpoints
End Get
End Property
Public ReadOnly Property SentenceCount() As Integer
Get
Return _sentencecount
End Get
End Property
Public ReadOnly Property ProbationUntil() As DateTime
Get
Return _probationuntil
End Get
End Property
Public Sub New(ByVal principle As IIdentity, ByVal roles() As String, _
ByVal points As Integer, ByVal sentences As Integer, ByVal probationTil As DateTime)
MyBase.New(principle, roles)
_totalpoints = points
_sentencecount = sentences
_probationuntil = FixDBNull(probationTil)
End Sub
End Class
在我的 Global.asaxApplication_AuthenticateRequest
函数中设置对象,如下所示:
HttpContext.Current.User = New CurrentUser(User, userRoles, _
points, sentenceCount, probationUntil)
在需要对象的地方直接转换,如下所示:
Dim thisUser As CurrentUser = DirectCast(Current.User, CurrentUser)
我也尝试过 CType,但它没有用......我的错误是
[InvalidCastException:无法将“System.Security.Principal.GenericPrincipal”类型的对象转换为“myProject.CurrentUser”类型。]
我在这里失去理智...... :(谢谢大家......
任何人?