9

この質問は以前に尋ねられたことがあるかもしれませんが、私の状況に正確に一致するものは見つかりません。

ASP.Net Web ページ ( Web フォームではない) と WebMatrix で WebMail ヘルパーを使用して Web サイトを作成しました。ユーザーは Web サイトにログインする必要があり、(理論的には) ユーザーがログアウトするまでログイン状態を維持する "Remember me" ボックスがあります。ユーザーがブラウザを閉じて 20 ~ 30 分以内に再度開いた場合、Web サイトはログイン状態を維持します。ただし、Web サイトにアクセスしない状態が 20 ~ 30 分続くと、ユーザーはログアウトされます。(余談ですが、この問題は WebMatrix テンプレートの「スターター サイト」でも存在するようです。)

複数のソリューションを試しましたが、その多くは Stack Overflow に投稿されていましたが、何も機能していないようです。

4

2 に答える 2

14

編集2

フォーム認証で使用される Cookie は ".ASPXAUTH" と呼ばれ、既定では 30 分後に有効期限が切れるように設定されています。

あなたに行き、要素web.configを見つけてください。authentication次のように、Cookie の有効期限 (分単位) を設定できます。

<system.web>
    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" 
               name="myCookie"                  <!-- optional, if you want to rename it -->
               timeout="2880" />                <!-- expires in 48 hours -->
    </authentication>
</system.web>

また

構成に失敗した場合は、次の記事を試してください:リンク

既存の認証チケットをすべてクリアして、カスタム チケットを作成する必要があります。remember meユーザーがオプションを選択した場合に実行する必要があるコードは、次のとおりです。

    if (rememberMe)
    {
        // Clear any other tickets that are already in the response
        Response.Cookies.Clear(); 

        // Set the new expiry date - to thirty days from now
        DateTime expiryDate = DateTime.Now.AddDays(30);

        // Create a new forms auth ticket
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, loginModel.UserName,  DateTime.Now, expiryDate, true, String.Empty);

        // Encrypt the ticket
        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

        // Create a new authentication cookie - and set its expiration date
        HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        authenticationCookie.Expires = ticket.Expiration;

        // Add the cookie to the response.
        Response.Cookies.Add(authenticationCookie);
    }
于 2013-08-06T14:39:38.973 に答える