Extend System.Web.HttpContext.User

I would like to extend the System.Web.HttpContext.User object (ASP.NET/VB.NET) so that it contains fields other than the name. I understand that I can create an object that inherits the System.Security.Principal.GenericPrincipal class, but how to save it in the Current.User object in a convenient way to use. those. I can do something like Current.User.UserID.

To achieve this, I created a workaround using | split the lines in the User.Name property, and then split them, but that got ridiculous.

Any suggestions?

Thanks!

EDIT : I 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 an object in my Global.asax function Application_AuthenticateRequestas follows:

HttpContext.Current.User = New CurrentUser(User, userRoles, _
 points, sentenceCount, probationUntil)

with a direct application where you need an object like this:

Dim thisUser As CurrentUser = DirectCast(Current.User, CurrentUser)

I also tried CType and it did not work ... my mistake

[InvalidCastException: "System.Security.Principal.GenericPrincipal" "myProject.CurrentUser".]

...:( , ...

-?

+3
2

Principal , Generic Principal, User .

ASP.Net MVC, -.

PostAuthenticateRequest ( Global.asax)

private void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
     SomePrincipal newUser = new SomePrincipal(User.Identity, tmpRoles);

                senderRef.Context.User = newUser;
                System.Threading.Thread.CurrentPrincipal = newUser;
}

, ( ), , Context.User , , HttpContext.

, !

+3

? , :

  • "" , . , "BasePage", System.Web.UI.Page.

  • ASP.net BasePage.

  • BasePage , , (, BasePage.FirstName, BasePage.LastName). User, , BasePage, . "BasePage.Customer". , BasePage.

  • OnInit() , HTTPContext.Current.User.Name .

, , ControlState, , , .

, ...

.

+2

Source: https://habr.com/ru/post/1709842/


All Articles