GVKun编程网logo

DWR中获取Session(dw select)

26

本文将介绍DWR中获取Session的详细情况,特别是关于dwselect的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于ashx中使用sessio

本文将介绍DWR中获取Session的详细情况,特别是关于dw select的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于ashx中使用session的方法(获取session值)、ashx文件中获取Session值、asp.net – 如何在EnableSessionState =“False”的请求中获取SessionID、ASP.NET在底层类库中获取Session C#类中获取Session的知识。

本文目录一览:

DWR中获取Session(dw select)

DWR中获取Session(dw select)

WebContext w=WebContextFactory.get(); HttpServletRequest r=w.getHttpServletRequest(); String sUrl = r.getRequestURL() + "?" + r.getQueryString(); String userName= (String) r.getSession(true).getAttribute( ParamResources.USER_NAME); String Ip= (String) r.getSession(true).getAttribute( ParamResources.REMOTE_IP);

ashx中使用session的方法(获取session值)

ashx中使用session的方法(获取session值)

WEB开发,在一般处理程序中,很容易得到 Request和Response对象,如:

复制代码 代码如下:

HttpRequest _request = context.Request;

HttpResponse _response = context.Response;

但是要得到 Session的值就没有那么简单了。

比如如果要在ashx得到保存在Session中的登录用户信息 Session["LoginUser"]

如果仅仅使用 context.Session["LoginUser"] 的话,是会报 “未将对象引用设置到对象的实例”的异常!

具体要使用下列方法:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace DtlCalendar.Mobile.Site.Manage
{
    /// <summary>
    /// DelApk 的摘要说明
    /// </summary>
    public class DelApk : IHttpHandler, IReadOnlySessionState
    {
        // IReadOnlySessionState :只读访问Session
        // IRequiresSessionState :读写访问Session
        public void ProcessRequest(HttpContext context)
        {
            string strID = context.Request["id"];
            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            int id;
            string user;
            if (int.TryParse(strID, out id) && IsLoged(context, out user))
            {
                string reslt = DataProvider.MobileDataProvider.CreateInstance().DelMApk(id).ToString();
                BLL.LogOprHelper.Instance.InsertMLog(user, BLL.LogOpr.Delete, "DelApk result:" + reslt);
                context.Response.Write(reslt);
            }
            else
            {
                BLL.LogOprHelper.Instance.InsertMLog(strID, BLL.LogOpr.Delete, "DelApk result:-1");
                context.Response.Write("-1");
            }
        }

        private bool IsLoged(HttpContext context, out string user)
        {
            BLL.User _User;
            if (context.Session["LoginUser"] != null)
            {
                _User = context.Session["LoginUser"] as BLL.User;
                if (_User != null)
                {
                    user = _User.Account;
                    return true;
                }
            }
            user = string.Empty;
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

您可能感兴趣的文章:
  • ASP.NET ASHX中获得Session的方法
  • Asp.net在ashx文件中处理Session问题解决方法
  • 在ashx文件中使用session的解决思路
  • ashx介绍以及ashx文件与aspx文件之间的区别
  • ashx文件的使用小结
  • aspx与ascx,ashx的用法总结
  • 后缀为 ashx 与 axd 的文件区别浅析
  • 基于.NET中:自动将请求参数绑定到ASPX、ASHX和MVC的方法(菜鸟必看)
  • *.ashx文件不能访问Session值的解决方法

ashx文件中获取Session值

ashx文件中获取Session值

在一般页面,可用 Request,Response获取对象,如下:

HttpRequest Request = context.Request; 

HttpResponse Response = context.Response;

但是要获取Session值,需得注意下。在ashx得到保存在Session中的登录名Session["userName"]

如这样获取context.Session["userAccount"]的话是会报 “未将对象引用设置到对象的实例”的异常

如要获取Session中的值 :引入命名空间SessionState,继承IRequiresSessionState 

如下:

1、引入命名空间:

using System.Web.SessionState;

2、实现IRequiresSessionState接口,具体如下  

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AddUserInfo : IHttpHandler,IRequiresSessionState 

    {

        public void ProcessRequest(HttpContext context)
        {

                if(context.Session["userName"] != null)

      {

        string name= context.Session["userName"].ToString();

      }

      //do something

    }

  }

asp.net – 如何在EnableSessionState =“False”的请求中获取SessionID

asp.net – 如何在EnableSessionState =“False”的请求中获取SessionID

我希望能够在WebMethod函数中获取当前经过身份验证的会话的SessionID,其中EnableSession = false.

我无法在此请求上设置EnableSession = true,因为另一个页面上的另一个(长时间运行的)请求会使SessionState保持锁定状态(EnableSessionState ==“True”而不是“Readonly”).

是否有一致的方法从ASP.NET会话cookie或Url获取SessionID用于无cookie会话?我可以自己编写代码,但我宁愿使用已经记录和测试过的函数.

非常感谢你,
弗罗林.

解决方法

似乎没有可以做到这一点的ASP.NET函数,所以我编写了一个我自己的hack,它的工作原理…现在;):

private string GetSessionID(HttpContext context)
{
  var cookieless = context.Request.Params["HTTP_ASPFILTERSESSIONID"];
  if (!string.IsNullOrEmpty(cookieless))
  {
    int start = cookieless.LastIndexOf("(");
    int finish = cookieless.IndexOf(")");
    if (start != -1 && finish != -1)
      return cookieless.Substring(start + 1,finish - start - 1);
  }
  var cookie = context.Request.Cookies["ASP.NET_SessionId"];
  if (cookie != null)
    return cookie.Value;
  return null;
}

ASP.NET在底层类库中获取Session C#类中获取Session

ASP.NET在底层类库中获取Session C#类中获取Session

类库中获取Session首先要添加引用

获取Session

复制代码 代码如下:

string user = (string)HttpContext.Current.Session["user"];

获取Page

复制代码 代码如下:

System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.Handler;

获取当前 Request Response 等对象都是在这里

复制代码 代码如下:

HttpResponse response = System.Web.HttpContext.Current.Response;
HttpRequest request = System.Web.HttpContext.Current.Request;

今天的关于DWR中获取Sessiondw select的分享已经结束,谢谢您的关注,如果想了解更多关于ashx中使用session的方法(获取session值)、ashx文件中获取Session值、asp.net – 如何在EnableSessionState =“False”的请求中获取SessionID、ASP.NET在底层类库中获取Session C#类中获取Session的相关知识,请在本站进行查询。

本文标签:

上一篇loner_li AJax 无刷新 省市县三级联动(webservice+sqlhelp)客户端控件版和 有刷新 web版

下一篇loner_li AJax 实现数据的 无刷新分页 实例(引用web服务文件和存储过程)