C#一般处理程序设置和读取session(session报错“未将对象引用设置到对象的实例”解决)

做自己的毕业设计登陆模块时,用到了session和cookie。在一般处理程序中处理session,一直报错。最后找到问题原因是需要调用 irequiressessionstate接口。

在ashx文件中,设置session直接用

HttpContext.Current.Session["UserCode"] = usercode
会报“未将对象引用设置到对象的实例”错误。搜索到很多资料说没有判断session对象是否存在,我是设置session对象,自然不是因为这个错误的。通过查找资料看到 http://www.cnblogs.com/tonysuen/archive/2010/12/07/1899595.html    的博文,修改了代码,调用了接口,完成了登陆功能。

  1. ashx文件中使用session需要using System.Web.SessionState;

  2. 根据需要调用IRequiresSessionState接口(读写)或者IReadOnlySessionState(只读)

    using System;
    using System.Collections.Generic;
    using System.Web;
    using DMS;
    using System.Data.SqlClient;
    using System.Data;
    using System.Text;
    using System.Web.SessionState;
     
     
    //  调用IRequiresSessionState接口
    public class login1 : IHttpHandler, IRequiresSessionState
        {
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string usercode = context.Request["usercode"].ToString();
                string type = context.Request["type"].ToString();
                string fun = context.Request["fun"].ToString();
                switch (fun)
                {
                    case "checkUser":
                        string pwd = context.Request["pwd"].ToString();
                        checkUser(type, usercode, pwd, context);
                        break;
                    default:
                        break;
                }
            }
              
             //验证用户信息(用户名和密码)
            public void checkUser(string type, string usercode, string pwd, HttpContext context)
            {
                string res = "";
                string sql = @"select count(1) from  " + type + "  t where t.usercode='" + usercode + "' 
                and t.userpwd ='" + pwd + "' ";
                try
                {
                    DataTable dt = sqlCon.getDt(sql);
                    string count = dt.Rows[0][0].ToString();
                    if (count == "1")
                    {
                    //如果有数据,则设置session
                        HttpContext.Current.Session["UserCode"] = usercode;
                        HttpContext.Current.Session["type"] = type;
                        HttpContext.Current.Session["UserPwd"] = pwd;
                        res = "ok";
                    }
                    else
                    {
                        res = "er";
                    }
                }
                catch (Exception e)
                {
                    res = "checkUser__" + e.ToString();
                }
                finally
                {
                    context.Response.Write(res);
                }
            }
        }

用作自己积累,同时希望能对碰到同样问题的同志有一些参考作用

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值