首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求一段登录验证程序解决思路

2013-03-14 
求一段登录验证程序想做一个登陆验证,请问该怎么写呢,我用ajax提交了用户名和密码到后台,怎么去判断?还有

求一段登录验证程序
想做一个登陆验证,请问该怎么写呢,我用ajax提交了用户名和密码到后台,怎么去判断?

还有就是验证通过以后跳转到一个网页,如果我直接输入这个网页的地址也可以访问,

怎么让没有登录不让访问这个网页?
[解决办法]
1\后台判断和正常判断是一样的 
if(Request["userName"]==....)
{
 Session["login"]=true;
 Response.Write("True");
 return
}
else{
 Response.Wirte("False")
 return 
}

2\前台接收为"true"就跳转
location.href='xxx.aspx' 或者 window.open('xxx.aspx','_top或其它');

3\每个要权限的页面都要进行Session判断
if(Session["login"]==null)
{
  ...没有登陆处理
}
[解决办法]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class entry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["userName"] = null;
        }
    }
    protected void btnEntry_Click(object sender, EventArgs e)
    {
        string userName = txtName.Text;
        string Pwd = txtPwd.Text;
        string sql = "select * from tb_user where userName='" + userName + "' and userPwd='" + Pwd + "'";
        if (dataOperate.seleSQL(sql) > 0)
      
        {
            Session["userName"] = txtName.Text;
            Response.Redirect("index.aspx");
        }
        else
        {
            RegisterStartupScript("", "<script>alert('登录失败!')</script>");
        }
    }
}


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] != null)        //判断用户是否登录    
        {
            bindBookInfo();                     //调用自定义方法用来绑定图书借阅排行
        }
        else
            Response.Redirect("entry.aspx");    //跳转到登录页面


    }
[解决办法]
1、怎么去判断----通过SQL去判断.
2、怎么让没有登陆不允许访问(也就是没有登陆则跳转到登陆页面)
  1、可以写一个基类 在基类里面进行判断登陆(判断Session
[解决办法]
Cokkie,没有则跳转到登陆页面)
  2、写一个类继承IHttpModule接口,并且在context_AcquireRequestState里面写上登陆判断,并
web.config里面httpModules里面添加该类的节点

热点排行