2011-11-15 18 views
1

可能性の重複:
.NET Application_BeginRequest - How to get User reference?asp.netログイン制御やGlobal.asaxの中にログインしたユーザー

私はASPのログイン制御に問題があります。 web.configファイルでForms認証モードを設定しました.Global.asax - > Application_BeginRequestメソッドでログに記録されたユーザーデータを取得します。

.aspxのコードスニペット:

<asp:Login OnAuthenticate="Login1_Authenticate" ID="LoginUser" runat="server"> 
    [...] 
    </asp:Login> 

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) 
    { 
     if (Membership.ValidateUser(LoginUser.UserName, LoginUser.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(LoginUser.UserName, LoginUser.RememberMeSet); 
     } 
    } 

Global.asaxのコードスニペット

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     var usr = HttpContext.Current.User; 
    } 

HttpContext.Current.Userプロパティがnullである - なぜ?

答えて

1

ユーザーがまだ認証されていないためですか? Application_BeginRequestは、認証クッキーの検証など、何か他のことが起こる前に、要求の始めに呼び出されます。

なぜこれをBeginRequestで行う必要がありますか?

関連する問題