0

Hi I am getting this error when trying to redirect my page to the login page.

public ActionResult Index() {
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
    //0 Username | 1 Fullname | 2 User Id | 3 Login Type | 4 Election Id
    string[] UserData = ticket.UserData.Split('|');

    if (UserData[3] == null)
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("login", "login");
    }

And the error is :

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 22: Line 23: public ActionResult Index() { Line 24:
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value); Line 25: //0 Username | 1 Fullname | 2 User Id | 3 Login Type | 4 Election Id Line 26: string[] UserData = ticket.UserData.Split('|');

Source File: C:\Elections\Elections2014\Controllers\ffs\ffsInventoryController.cs
Line: 24

Craig W.
  • 17,838
  • 6
  • 49
  • 82

1 Answers1

0

Your variable ticket seems to be null. That's why you get a NullPointerException in the moment you want to split the UserData. FormsAuthentication.Decrypt() returns null if the encryptedTicket parameter is not a valid ticket.

Have a look at: FormsAuthentication.Decrypt Method (String)

Request.Cookies[FormsAuthentication.FormsCookieName].Value should return a valid encryptedTicket-String. In your case it seems to be that is doesn't return a valid string.

rweisse
  • 820
  • 10
  • 26
  • So how should I handle this if it is returning a null value? – Nalini Raghavan Sep 27 '17 at 15:51
  • Have a look at the example code provided by microsoft about how to loop cookies: https://msdn.microsoft.com/en-us/library/system.web.httprequest.cookies(v=vs.110).aspx#Anchor_2 Maybe this will help you understanding your mistake. – rweisse Sep 27 '17 at 15:57