使用Session验证用户登录?怎么个验证法?学习下。

解决方案 »

  1.   


    //楼主是要这种效果吗?
    登陆时,用户名和密码都正确
    在程序加一行如下的代码:
    Session["user"] = "wzf";在其它页面中的Page_Load事件加下面代码
    if (Session["user"] != null)
    {
            if (Session["user"].ToString() == "wzf")
            {
                    //符合要求,什么也不干就行了.
            }
            else
            {
                    Response.Redirect("http://www.sina.com.cn");//不符合要求,就转到其它页面,如可以转到你的登陆页面
             }
    }
    else
    {
                Response.Redirect("http://www.sina.com.cn");//不符合要求,就转到其它页面,如可以转到你的登陆页面
    }//当你要登陆出来时就用下面代码
    Session["user"] = null;//其实不要这一句也可以,Session是有时间的,20分钟一直不操作网页就会失效.
      

  2.   


    //楼主是要这种效果吗?
    登陆时,用户名和密码都正确
    在程序加一行如下的代码:
    Session["user"] = "wzf";在其它页面中的Page_Load事件加下面代码
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["user"] != null)
    {
            if (Session["user"].ToString() == "wzf")
            {
                    //符合要求,什么也不干就行了.
            }
            else
            {
                    Response.Redirect("http://www.sina.com.cn");//不符合要求,就转到其它页面,如可以转到你的登陆页面
             }
    }
    else
    {
                Response.Redirect("http://www.sina.com.cn");//不符合要求,就转到其它页面,如可以转到你的登陆页面
    }
    }
    //当你要登陆出来时就用下面代码
    Session["user"] = null;//其实不要这一句也可以,Session是有时间的,20分钟一直不操作网页就会失效.
      

  3.   

    登陆成功
    Session["username"]= "jcrjia"其他页面if(string.IsNullOrEmpty(Session["username"].ToString()))
       Response.Write("<script>alert('请登陆!')</script>");
    else
       Response.Write("<script>alert('OK!')</script>");
      

  4.   

    公共类#region 后台页面身份验证
    /// <summary>
    /// 内网登录,防止不登陆直接进入后台界面,也防止session变量过期,以便转入登录界面
    /// </summary>
    /// <param name="page">page对象</param>

    /// <param name="IsPopPage">是否是弹出窗体</param>
    /// /// <param name="pageDepth">相对于login.aspx ,本页所属深度 0 为同一层</param>
    public static void IsLogin(Page page,bool IsPopPage,int pageDepth )
    {
    if(page.Session["userID"] == null || page.Session["userName"] == null || page.Session["roleID"] == null )
    {
    if (IsPopPage == true)
    {//如果是弹出页面
    System.Web.HttpContext.Current.Response.Write("<br><br><p align='center'><table><tr><td><img src='Images/timeover.gif' border=0></td><td width='20'></td><td valign='middle'><span style='FONT-SIZE: 10pt;'>用户登录信息已过期,请关闭所有窗口,重新登录。</span></td></tr></table></p><br>");
    }
    else
    {//如果是正常页面
    string url = "Login.aspx";
    for (int i = 0 ; i< pageDepth ; i++)
    {
    url = "../" + url;
    }
    System.Web.HttpContext.Current.Response.Write("<script>window.top.location.reload('" + url + "')</script>");
    }
    page.Response.End();
    }
    }
    页面只加上这句话就可以了sdxf.IdentityValidate.IsLogin(this.Page,false,1);具体参数见上面
      

  5.   

    小小PS下:4楼
    Session["username"].ToString()
    有时会出错:空引用.
    当Session["username"] = null时.
      

  6.   

    强烈不建议你使用session作为验证。请看这里:http://www.baidu.com/s?wd=%C9%ED%B7%DD%D1%E9%D6%A4+site%3Awww.svnhost.cn