首先需要配置Web.config文件,配置內容如下:
<system.web>
<!--指定認證方式為Forms-->
<authentication mode="Forms">
<!--loginUrl的值為你登陸頁面url-->
<forms loginUrl="~/User/Login" slidingExpiration="true"></forms>
</authentication>
</system.web>
配置好Web.config后在你的用戶登錄驗證通過的位置加上一句代碼:
//sUserName之為你的用戶名
FormsAuthentication.SetAuthCookie(sUserName, false);
快去試試吧!
