0

before i didnt have this problem, i updated my windows from 7 to 10 , and then installed sql server 2012 and vs 2012. now i open my project and see this error message in site.master. how can i fix this problem ?? thank you

  protected void master_Page_PreLoad(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Set Anti-XSRF token
        ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
        ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
    }
    else
    {
        // Validate the Anti-XSRF token
        if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
            || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
        {
            throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
        }
    }
}

i see error on this line:

 ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;

here is exception details:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=App_Web_iq1yfyyi
  StackTrace:
       at Site.master_Page_PreLoad(Object sender, EventArgs e) in e:\Asp.Net projects\Basije daneshgah marvdasht\Site.master.cs:line 56
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Web.UI.Page.OnPreLoad(EventArgs e)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
Mohammad
  • 1
  • 4

1 Answers1

0

The problem is clearly that you have changed the environment where you worked. I am not sure whether the version of Windows is to blame, from the information you have given this is not clear to me. So, first things first:

The line of

ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;

was successfully executed, so it is clear that ViewState is existent and AntiXsrfTokenKey is defined as well. Your problem is at the line of

ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;

So, AntiXsrfUserNameKey might be null or Context might be null or Context.User might be null or Context.User.Identity might be null. You can ensure that the error you have got will no longer exist by properly checking everything not to be null, but depending on the seriousness of the problem, this might be less than enough for a solution. You will need to find out which of the candidates I have given is actually null and you will need to find out why. It might come from your AppFile.config, or the web.config or the compatibility of your project with the ASP.NET version you are using, or from IIS, so the error can come from multiple possible sources.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175