2016-09-29 9 views
0

、私はUser.Identity.Nameスローオブジェクト参照がインスタンスに設定されていない

Anonymous Authentication : Disabled 
Windows Authentication : Enabled 

を持っていると私は

Object reference not set to an instance 
を取得しています私は、IISの開発設定では、このコード

public UserProfile spUserProfileGet() 
     { 
      // this line throw : Object reference not set to an instance 
      var user = HttpContext.Current.User.Identity.Name; 

      //... 

     } 

を持っています

なぜですか?これまで多くのプロジェクトで問題なくこのクラスを使用しています:/

HttpContext.Current.UserNULLですが、どうしてですか?

+1

[NullReferenceExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix -it) – DavidG

+0

はい、UserオブジェクトはNULLですが、なぜですか? – Muflix

+0

重複した質問を読むと、それを正確に把握する方法がわかります。 – DavidG

答えて

0

問題は、私がこのオブジェクトをまだ含んでいないApplication_BeginRequest()からHttpContext.Current.Userをglobal.asaxから呼び出したことです。ここでアプリケーションのライフサイクルhttps://msdn.microsoft.com/en-us/library/ms178473.aspxの説明があり、コードを Application_PostAuthenticateRequestにコピーすると、今すぐ動作します。

残る問題は、Application_PostAuthenticateRequestが2回トリガーされることです。

関連する問題