扩展 System.Web.HttpContext.User
我想扩展 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.asax Application_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”类型。]
我在这里失去了理智...:(谢谢大家...
有人吗?
I would like to extend the System.Web.HttpContext.User object (ASP.NET/VB.NET) so that it contains other fields besides just Name. I understand I can create an object that inherits the System.Security.Principal.GenericPrincipal class, but how do I store that in the Current.User object in a usable fashion. ie, I can do something like Current.User.UserID
.
So far to achieve this I've created a kludgy workaround by using | delimited strings in the User.Name property and then splitting them, but it's getting kind of ridiculous.
Any suggestions?
Thanks!
EDIT: I have tried the following to no avail:
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
setting the object in my Global.asax Application_AuthenticateRequest
function like so:
HttpContext.Current.User = New CurrentUser(User, userRoles, _
points, sentenceCount, probationUntil)
with a direct cast wherever the object is needed like so:
Dim thisUser As CurrentUser = DirectCast(Current.User, CurrentUser)
i also tried CType and it didn't work... my error is
[InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'myProject.CurrentUser'.]
i'm losing my mind here ... :( thanks guys...
anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用所需的属性创建自己的Principal 类,该类继承自通用Principal,然后将当前上下文的User 属性设置为该类型的用户。
下面的示例适用于 ASP.Net MVC,但类似的方法也可用于 Web 表单。
您可以在用户经过身份验证后(在 Global.asax 中)在 PostAuthenticateRequest 中执行此操作,
然后您可以在页面(或控制器)的基类中添加属性或方法,例如要包装的属性或方法并将 Context.User 主体键入您的主体类型,并确保调用它而不是调用 HttpContext 上的主体。
可能还有其他解决方案!
You can create your own Principal class with the required properties, that inherits from a Generic Principal, and then set the User property of your Current Context to be the a user of that type.
The example below is for ASP.Net MVC but a similar approach could be used with webforms.
You can do this in the PostAuthenticateRequest after a user is authenticated (in the Global.asax)
You could then add a property or method in a base class of your page (or controller) for example that to wrap and type the Context.User principal to your Principal type and make sure you call it rather than calling the one on the HttpContext.
There are probably other solutions too!
这种方法对你有用吗? 它看起来有点复杂,但设置起来确实不需要太长时间:
创建您自己的“基”类,并让您的页面继承它。 例如,创建一个名为“BasePage”的基类,它继承自 System.Web.UI.Page。
让 ASP.net 页面从新的 BasePage 类继承。
在 BasePage 类中,您可以拥有一个公共属性,其中包含要为用户存储的额外字段(例如 BasePage.FirstName、BasePage.LastName)。 更好的是,创建一个包含额外字段的 User 对象,并通过 BasePage 公开该对象,例如。 “基本页面.客户”。 如果您计划稍后扩展 BasePage,这可以使事情保持整洁。
然后,您可以重写基类的 OnInit() 以检查 HTTPContext.Current.User.Name 属性,并从数据库中获取必要的信息来初始化您的自定义属性。
您可以修改代码,以便在每次刷新页面时都不需要访问数据库,方法是使用 ControlState 检查自定义字段是否具有值,然后再从数据库中再次填充它们。
希望这有帮助......
理查德。
Would this approach work for you? It looks a little involved but it really doesn't take too long to setup:
Create a 'base' class of your own, and have your pages inherit from that. For example, create a base class called 'BasePage' which inherits from System.Web.UI.Page.
Have your ASP.net pages inherit from your new BasePage class.
In the BasePage class, you can have a public property which contains the extra fields you want to store for your user (eg. BasePage.FirstName, BasePage.LastName). Better still, create a User object containing the extra fields, and expose that via BasePage, eg. "BasePage.Customer". This keeps things tidy if you plan to extend BasePage later.
You can then override the OnInit() of the base class to check for HTTPContext.Current.User.Name property, and fetch the necessary info from your DB to initialise your custom properties.
You can modify the code so that it won't need to hit the database each time the page is refreshed by using ControlState to check whether the custom fields have values before populating them again from the database.
Hope this helps...
Richard.