我现在准备用COOKIES判断会员的登陆情况,我在网站根目录中的web.config中设置
<authentication mode="Forms" >
<forms name=".MyC" loginUrl="/member/login.aspx" protection="All"/>
</authentication>
<authorization>
<allow users="*" /> <!-- 允许所有用户 -->
</authorization>
然后在会员文件夹中的web.config中
<authorization>
<allow users="MyUser" />
<deny users="*" />
</authorization>
然后在登陆页面的程序如下:
HttpCookie MyCookie =new HttpCookie("Member_Id");
MyCookie.Value = psi.Id.ToString();
MyCookie.Expires = System.DateTime.Now.AddHours(1);
Response.Cookies.Add(MyCookie);
string Reurl=FormsAuthentication.GetRedirectUrl(accounts, false);
if (Reurl.EndsWith(URL_MEMBERSIGNIN) || Reurl=="/default.aspx")
{
FormsAuthentication.SetAuthCookie("MyUser", false);
HttpContext.Current.Response.Redirect(URL_DEFAULT, true);//应该指向会员管理界面首页
}
else
{
FormsAuthentication.SetAuthCookie("MyUser", false);
HttpContext.Current.Response.Redirect(Reurl, true);
}
我运行后却不能保存COOKIES,一旦我关闭浏览齐重先打开还是需要登陆,请问我上面的做法有和不妥,在线等待……
<authentication mode="Forms" >
<forms name=".MyC" loginUrl="/member/login.aspx" protection="All"/>
</authentication>
<authorization>
<allow users="*" /> <!-- 允许所有用户 -->
</authorization>
然后在会员文件夹中的web.config中
<authorization>
<allow users="MyUser" />
<deny users="*" />
</authorization>
然后在登陆页面的程序如下:
HttpCookie MyCookie =new HttpCookie("Member_Id");
MyCookie.Value = psi.Id.ToString();
MyCookie.Expires = System.DateTime.Now.AddHours(1);
Response.Cookies.Add(MyCookie);
string Reurl=FormsAuthentication.GetRedirectUrl(accounts, false);
if (Reurl.EndsWith(URL_MEMBERSIGNIN) || Reurl=="/default.aspx")
{
FormsAuthentication.SetAuthCookie("MyUser", false);
HttpContext.Current.Response.Redirect(URL_DEFAULT, true);//应该指向会员管理界面首页
}
else
{
FormsAuthentication.SetAuthCookie("MyUser", false);
HttpContext.Current.Response.Redirect(Reurl, true);
}
我运行后却不能保存COOKIES,一旦我关闭浏览齐重先打开还是需要登陆,请问我上面的做法有和不妥,在线等待……
你得缺少这个环节
参考代码:
Login.cs
Page_Load{
if(Request.Cookies["Member_Id"]!=null)
{
//try get user data from Cookie at frist
}
}