星空网 > 软件开发 > ASP.net

ajax配合一般处理程序(.ashx)登录的一般写法

前端

<html 

 后端:

public class User : IHttpHandler, IRequiresSessionState{  public void ProcessRequest(HttpContext context)  {    context.Response.ContentType = "text/plain";    StringBuilder _strContent = new StringBuilder();    if (_strContent.Length == 0)    {      string _strAction = context.Request.Params["action"];      if (string.IsNullOrEmpty(_strAction))      {        _strContent.Append("{\"msg\": \"0\", \"msgbox\": \"禁止访问!\",\"rows\": []}");      }      else      {        switch (_strAction.Trim().ToLower())        {          case "userlogin": _strContent.Append(UserLogin(context)); break;          default: break;        }      }    }    context.Response.Write(_strContent.ToString());  }  private string UserLogin(HttpContext context)  {    string result = "";    string _username = context.Request.Form["username"];    string _password = context.Request.Form["password"];    Model.cmUser model = new Model.cmUser();    if (context.Session["UserModel"] != null)    {//当前浏览器已经有用户登录 判断是不是当前输入的用户      model = (Model.cmUser)context.Session["UserModel"];      if (model.Name != _username)      {        result = "{\"msg\": \"0\", \"msgbox\": \"此浏览器已经有其他用户登录!\"}";      }      else      {        result = "{\"msg\": \"1\", \"msgbox\": \"登录成功!\"}";      }    }    else    {      BLL.cmUser bll = new BLL.cmUser();      string strWhere = string.Format("[Name]='{0}'", _username);// and [Password]='{1}', _password      DataTable dt = bll.GetList(1, strWhere, " ID ").Tables[0];      if (dt != null)      {//用户和密码正确        int _userid = 0;        int.TryParse(dt.Rows[0]["ID"].ToString(), out _userid);        model.ID = _userid;        model.Name = dt.Rows[0]["Name"].ToString();        int _type = 0;        int.TryParse(dt.Rows[0]["Type"].ToString(), out _type);        model.Type = _type;        context.Session["UserModel"] = model;        result = "{\"msg\": \"1\", \"msgbox\": \"登录成功!\"}";      }      else      {        result= "{\"msg\": \"0\", \"msgbox\": \"用户名或密码错误!\"}"; ;      }    }    return result;  }  public bool IsReusable  {    get    {      return false;    }  }}

 




原标题:ajax配合一般处理程序(.ashx)登录的一般写法

关键词:ajax

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流