locked
intermittent fail - Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. RRS feed

  • Question

  • User-962670764 posted

    This does not always happen. The error occurs on this line.

    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

    At this time the user if filling out a form that can bet used several times. The first one usually works, but after that  this error may be thrown when  the form is submitted .

     HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            rider rd = new rider();
            int rid;
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            rid = Convert.ToInt32(authTicket.UserData);

    The form submission causes the page (master) to go through this code every time it is submitted. Is  that a problem?

    Monday, May 25, 2015 11:36 AM

Answers

  • User1564875471 posted

    This does not always happen. The error occurs on this line.

    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

    This means that the authCookie is sometimes have null value which might be the case when the cookie is missing (expired or the user is logged out / not logged in). What you can do is the check Request.IsAuthenticated and if it's evaluated to true, you perform the above code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 25, 2015 12:19 PM
  • User61956409 posted

    Hi DieselDave,

    Thanks for your post.

    As for your problem, I agree with anas. If authCookie is null, you will get this error. Before you perform your code, please check if authCookie is null.

    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                    
    if (authCookie != null)
    {
        rider rd = new rider();
        int rid;
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        rid = Convert.ToInt32(authTicket.UserData);
        //......
    }
    else
    {
        //redirect to login page
    } 
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2015 3:06 AM

All replies

  • User1564875471 posted

    This does not always happen. The error occurs on this line.

    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

    This means that the authCookie is sometimes have null value which might be the case when the cookie is missing (expired or the user is logged out / not logged in). What you can do is the check Request.IsAuthenticated and if it's evaluated to true, you perform the above code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 25, 2015 12:19 PM
  • User61956409 posted

    Hi DieselDave,

    Thanks for your post.

    As for your problem, I agree with anas. If authCookie is null, you will get this error. Before you perform your code, please check if authCookie is null.

    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                    
    if (authCookie != null)
    {
        rider rd = new rider();
        int rid;
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        rid = Convert.ToInt32(authTicket.UserData);
        //......
    }
    else
    {
        //redirect to login page
    } 
    

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2015 3:06 AM