Membershipprovider:会话结束时自动注销

时间:2010-09-09 15:23:59

标签: asp.net asp.net-membership forms-authentication remember-me

我遇到一些问题,让我的网站在会话结束时自动注销经过身份验证的用户(用户关闭浏览器)。

这就是我在web.config中的内容:

<authentication mode="Forms">
    <forms name="AuthCookie" protection="All" loginUrl="~/default.aspx" path="/" cookieless="UseCookies" timeout="2592000"/>
</authentication>

<authorization>
    <allow users="?" />
</authorization>

<membership defaultProvider="ASPPGSqlMembershipProvider" userIsOnlineTimeWindow="20">
    <providers>
        <clear />
        <add name="AspNetSqlMemberShipProvider" applicationName="umbraco4" type="System.Web.Security.SqlMembershipProvider" connectionStringName="UmbracoDb" requiresUniqueEmail="true" enablePasswordReset="true" enablePasswordRetrieval="false"/>
        <add name="UsersMembershipProvider" applicationName="umbraco4" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" />
        <add name="ASPPGSqlMembershipProvider" applicationName="umbraco4"
            passwordStrengthRegularExpression="" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0"
            enablePasswordRetrieval="false"
            enablePasswordReset="true"
            requiresQuestionAndAnswer="false"
            requiresUniqueEmail="true"
            forumUpfileFolderPath="D:\www\files"
            type="ASPPG.MembershipProviders.ASPPGSqlMembershipProvider, ASPPGSiteIntegrationPackage"/>
    </providers>
</membership>

这是我登录用户的方式:

if (Membership.ValidateUser(txtUserName.Text, txtPasssword.Text)) {
    HttpCookie cookie = FormsAuthentication.GetAuthCookie(txtUserName.Text, false);
    cookie.Expires = DateTime.Now.AddDays(1);
    cookie.Domain = ConfigurationManager.AppSettings["Level2DomainName"];
    HttpContext.Current.Response.Cookies.Add(cookie);
    Response.Redirect(Request.Url.ToString());
}

当我关闭浏览器时,用户仍然登录。如何通过选项让网站忘记用户,以便用户自己决定网站是否应该记住?

提前致谢:)

中号

1 个答案:

答案 0 :(得分:5)

您是否尝试过不设置cookie.Expires或至少将其设置为DateTime.MinValue以便用户不想被“记住”?

来自MSDN

  

将Expires属性设置为MinValue会使其成为会话Cookie,这是其默认值。