Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forms Authentication Not Validating User properly

I have this code when to sign in User , that string sUserData is properly set.

  Dim sUserData As String = HttpContext.Current.Request.Cookies("UserID").Value & "|" & HttpContext.Current.Request.Cookies("UserName").Value & "|" & HttpContext.Current.Request.Cookies("UserEmail").Value

  Dim fat As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, _
     HttpContext.Current.Session("UserID"), DateTime.Now, _
     DateTime.Now.AddDays(6), True, sUserData, _
     FormsAuthentication.FormsCookiePath)
  HttpContext.Current.Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)))

Then I have code where I check if the user if signed in in a Shared (static) method in a Public Class like this :

 If HttpContext.Current.User.Identity.IsAuthenticated Then
 EndIf

And that works just fine , but if I put the same line in Page_load instead of a Shared Method of a class it will never go into this If statement

  If HttpContext.Current.User.Identity.IsAuthenticated Then
  EndIf

Why is this happening , and is there some way to re-write this to work in the code-behind Page_Load instead of having to put it in a class ,The class is used in a header to allow access to certain pages - so that works fine. But I need another way of authentication of user on Default page to change labels and buttons based on weather the user is logged in or not , and this can not be done in a class.

like image 955
Scott Selby Avatar asked Nov 04 '22 19:11

Scott Selby


1 Answers

Have you tried putting the page event overrides into an actual page event override (i.e. OnLoad) instead of the Page_Load event hook implementation? More performant (fewer layers of invoke), slight difference in life-cycle which may suit your needs and may distill the cause of these symptoms.

There may be a sequencing issue / race condition if the context of the static method call and the Page_Load, I think Wiktor Zychla pointed you in the direction of fiddler already.

like image 151
Steve Avatar answered Nov 15 '22 06:11

Steve