2011-08-10 9 views
0

私はasp.net 4.0とフォーム認証を使用しています。 ユーザーが認証されているかどうかを確認するには、私はUser.Identity.IsAuthenticatedを使用します。 ほとんどの場合、それは完璧に動作しますが、私はどのように知っているのですか?ユーザーが認証を持っていてもfalseを返すことがあります。 私のweb.configファイル:Global.asaxの中User.Identity.IsAuthenticatedはfalseを返します。

<authentication mode="Forms"> 
    <forms name=".xyz" loginUrl="~/" timeout="120" protection="All" path="/" slidingexpiration=true/> 
</authentication> 

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    string cookieName = FormsAuthentication.FormsCookieName; 
    HttpCookie authCookie = Context.Request.Cookies[cookieName]; 

    if (authCookie == null) 
    { 
     return; 
    } 
    FormsAuthenticationTicket authTicket = null; 
    try 
    { 
     authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
    } 
    catch 
    { 
     return; 
    } 
    if (authTicket == null) 
    { 
     return; 
    } 
    string[] roles = authTicket.UserData.Split(new char[] { '|' }); 
    FormsIdentity id = new FormsIdentity(authTicket); 
    GenericPrincipal principal = new GenericPrincipal(id, roles); 

    Context.User = principal; 
} 

とログインページで:

FormsAuthenticationTicket authTick = new FormsAuthenticationTicket(1, email.Text, DateTime.Now, DateTime.Now.AddDays(360), true, password.Text, FormsAuthentication.FormsCookiePath); 
string encriptTicket = FormsAuthentication.Encrypt(authTick); 

HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encriptTicket); 
authCookie.Expires = DateTime.Now.AddDays(360); 
Response.Cookies.Add(authCookie); 

を、私はまた、すべての5分でAJAXリクエストを使用しています。セッションを有効に保ちます。また、スライディングエクスプレッション値のためにauthタイムアウトをリセットします。 私は何が間違っているのか分かりません。同一のセッションで、同じ分に、他のすべてのページに対してtrueを返す場合でも、1ページに対してfalseを返します。このエラーは一度もありませんが、私の訪問者はその問題について主張しています。

+0

Context.Request.IsAuthenticatedを試しましたか? – VJAI

+0

番号。それはより信頼できますか? – genesistr

答えて

2

問題が見つかりました。問題はwww.address.comとaddress.comの違いに関する問題でした。 wwwバージョンはサブドメインのようにふりをして、新しいセッションと認証を作成します。ユーザーがwww接頭辞なしで来たときにサーバーがwwwアドレスにリダイレクトすると、エラーが発生します。私はそれを解決するために書き直すURLを試みます。

+0

私にとっては、忘れてしまったのはWeb.configトランスフォームでした。 –

+0

@DaviddCeFreitas、あなたの解決策の詳細を教えてください –

+0

@ebramtharwat、私はそれらの生産/開発web.config.debugの1つをビルド後に値を変更していた変換ファイルを持っていました。ビジュアルスタジオのブラウザでweb.configファイルを展開するか、ビルド時に値を変更または変更するweb.config。*ファイルが他にある場合は、プロジェクトフォルダにチェックインしてください。 –

関連する問題