asp.net Forms登录核心方法

登录核心方法:

 private void Signin(string curUserId)
{

System.Web.Security.FormsAuthenticationTicket tk = new FormsAuthenticationTicket(1,
                    curUserId,
                    DateTime.Now,
                    DateTime.Now.AddDays(1),
                    true,
                    "",
                    System.Web.Security.FormsAuthentication.FormsCookiePath
                    );

            string key = System.Web.Security.FormsAuthentication.Encrypt(tk); //得到加密后的身份验证票字串 

            HttpCookie ck = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, key);
            //ck.Domain = System.Web.Security.FormsAuthentication.CookieDomain;  // 这句话在部署网站后有用,此为关系到同一个域名下面的多个站点是否能共享Cookie
            HttpContext.Current.Response.Cookies.Add(ck);
            HttpContext.Current.Response.Cookies["MenuStyle"].Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies["PageSize"].Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies["TableSink"].Expires = DateTime.Now.AddDays(1);
}

文章:ASP.NET Forms验证详解

<authentication mode="Forms">  
 <forms   
   loginUrl="AdminLogin.aspx"  
   timeout="30"   
   name=".ASPXAUTH"   
   path="/"   
   requireSSL="false"   
   cookieless="UseDeviceProfile"   
   defaultUrl="default.aspx"   
   slidingExpiration="true"   
   protection="All"   
   enableCrossAppRedirects="false">  
 </forms>  
</authentication> 

以上代码中,loginUrl为用户登录网页,如果省略,asp.net将使用网站根目录下的login.aspx为登录页面。

timeout设置登录超时时间为30分钟。name为存储身份验证票据的Cookie名,默认值为“.ASPXAUTH” 。

path为存储身份验证票据的Cookie的路径,默认值为“/”。

requireSSL为存储身份验证票据的Cookie是否使用SSL加密传输,默认为false。

cookieless为浏览器不支持Cookie时的存储身份验证票据的传递方式,默认值为“UseDeviceProfile”,即自动检测浏览器是否支持Cookie,如果浏览器支持Cookie则使用Cookie传递身份验证票据,如果浏览器不支持Cookie则使用URL传递身份验证票据。

defaultUrl为登录后默认跳转的网页,默认值为“default.aspx”。slidingExpiration为是否以执行可变的会话生存期,默认值为true。

protection为Cookie的加密类型,默认值为“All”,即对Cookie同时使用数据验证和加密方法,其中数据验证算法由<machineKey>节点中设置。enableCrossAppRedirects是否将通过身份验证的用户重新定向到其它Web应用程序的URL中,默认值为false。

原文地址:https://www.cnblogs.com/Tpf386/p/9578792.html