62,016
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 退出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtn_LoginOut_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();//从浏览器删除Forms身份验证票证
Response.Redirect("Login.aspx");//跳转到登陆页面
}
//之前的登录注销了吗?退出时主动调用下面的方法注销凭证
FormsAuthentication.SignOut();
//你可以通过下面的代码检测当前的登录状态
Response.Write(Page.User.Identity.IsAuthenticated);
//获取界面的值
string UserName = txtUserName.Text;//用户名
string Password = txtPassword.Text;//密码
string checkcode = Session["Code"].ToString();
string code = txtCheckCode.Text;
try
{
if (code != checkcode)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('验证码错误!')", true);
return;
}
else
{
if (UserInfoLogic.checkUser(UserName, Password))
{
Session["login_name"] = txtUserName.Text;//将用户名存入Session
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(20), true, "");
string encryptTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket);
Response.Cookies.Add(cookie);
if (Request.QueryString["ReturnUrl"] == null)
{
Response.Redirect("Home.aspx");
}
else
{
Response.Redirect(Request.QueryString["ReturnUrl"]);
}
//ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert(\'登录成功!\');setTimeout(function(){location.href='Home.aspx'},3000);</script>");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('登录失败!')", true);
txtUserName.Text = "";
txtCheckCode.Text = "";
Response.Redirect("Login.aspx");
}
}
}
catch (Exception err)
{
Response.Write(err.ToString());
}
}
<authentication mode="Forms">
<forms loginUrl="Login.aspx" name=".ASPXAUTH"></forms>
<!--指定如果找不到任何有效的身份验证 Cookie,将请求重定向到的用于登录的 URL。-->
</authentication>
<deny users = "*"/><!--这个表示禁用所有用户,包括登录成功的-->
<!--改成-->
<deny users = "?"/> <!--?表示未验明身份的用户,即未登录的-->
<deny users = "* "/>
<allow users = "admin " verbs = "* "/>