0

I am posting this here after exhausting out every possible solution that could resolve the issue I am facing with my custom implementation of Forms Authentication. To give you a background of what I did so far... I was trying to implement the accepted solution from the following thread.. ASP.NET MVC - Set custom IIdentity or IPrincipal

So, I changed my Web.Config to allow for forms authentication. This redirects the user to the login page when the first request comes in. This is how my Web.Config looks right now.

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
        <forms loginUrl="Account/login" timeout="30" slidingExpiration="true"></forms>
    </authentication>
    <httpModules>
        <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
</system.web>

In the controller after the user is validated...

Dim serializer = New JavaScriptSerializer()
Dim serializableModel = New With {
    .SchoolYearId = _userModel.SchoolYearId,
    .AccessLevel = _userModel.AccessLevel,
    .UserId = _userModel.UserId,
    .FirstName = _userModel.FirstName,
    .LastName = _userModel.LastName,
    .SchoolYear = _userModel.SchoolYear,
    .Role = _userModel.Role
}
'.Identity = _userModel.Identity

Dim userData As String = serializer.Serialize(serializableModel)
Dim authenticationTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, _userModel.UserId, DateTime.Now, DateTime.Now.AddMinutes(15), False, userData)
Dim encTicket As String = Security.FormsAuthentication.Encrypt(authenticationTicket)
Dim faCookie As HttpCookie = New HttpCookie(Security.FormsAuthentication.FormsCookieName, encTicket)
Response.Cookies.Add(faCookie)
Return RedirectToAction("Add")

Now in the global.asax file, in the post authenticate section..

Dim authCookie As HttpCookie = Request.Cookies(System.Web.Security.FormsAuthentication.FormsCookieName)
If (Not IsNothing(authCookie)) Then
    Dim authTicket As FormsAuthenticationTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value)
    Dim serializer = New JavaScriptSerializer()
    Dim deSerializedModel = serializer.Deserialize(Of UserModel)(authTicket.UserData)
    Dim userModel As IUserModel = New UserModel
    userModel.SchoolYear = deSerializedModel.SchoolYear
    userModel.SchoolYearId = deSerializedModel.SchoolYearId
    userModel.AccessLevel = deSerializedModel.AccessLevel
    userModel.UserId = deSerializedModel.UserId
    userModel.FirstName = deSerializedModel.FirstName
    userModel.LastName = deSerializedModel.LastName
    userModel.SchoolYear = deSerializedModel.SchoolYear
    userModel.Role = deSerializedModel.Role
    HttpContext.Current.User = userModel

End If

So, once the user the authenticated, the code goes through the postauthenticaterequest block and right after it exits the postauthenticaterequest block, the following error pops up which has been driving me crazy.

http://imgur.com/a/XGMnb

I did go through some of the problems that other users faced but this is something I couldnt find much help on. How do I go about solving this? This happens locally and not the webserver.

Community
  • 1
  • 1
Cupid
  • 48
  • 2
  • 10
  • 1
    Can you show `Application_PostAuthenticateRequest` method code? Probably you have `HttpContext.Current.User.Identity.IsAuthenticated` set to `False` at certain point, hence `HttpContext.Current.User` is set to `Nothing` and throwing NRE. – Tetsuya Yamamoto Mar 17 '17 at 04:04
  • That's posted above. That is the last section you see in my post – Cupid Mar 17 '17 at 04:24
  • @TetsuyaYamamoto you were right I overlooked this part. I wasn't setting the generic identity in the post authenticate request. I wish I could mark your answer as the right one. I will wait for sometime before closing the case. – Cupid Mar 17 '17 at 13:29

0 Answers0