如何在asp.net web服务2的webMethods之间交换会话或Cookie变量 [英] how to exchange session or cookie variables between two webmethods in asp.net webservices

查看:119
本文介绍了如何在asp.net web服务2的webMethods之间交换会话或Cookie变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的webmethod我已经使用会话变量两个webMethods的,但其显示在第二的WebMethod空,我们可以用cookie里备用会话存储和检索用户名


$之间交换的用户名变量b $ b

  [的WebMethod(EnableSession =真)] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
公共BOOL SubmitList1(用户名字符串)
{
会议[的User_Name] =用户名;
.........
......

}
[的WebMethod(EnableSession =真)]
[ ScriptMethod(ResponseFormat = ResponseFormat.Json)]
公共BOOL addresslisting(字符串关键字)
{

串的用户名= Context.Session [的User_Name]的ToString()。
........
.........
}


解决方案

免责声明:我认为这依赖于会话状态的Web服务是完全错误的,因为Web服务应该是无状态的。但是:



目前 HTTP ://msdn.microsoft.com/en-us/library/aa480509.aspx 你可以阅读关于如何在Web服务中使用ASP.NET会话:




  1. 确保该/configuration/system.web/sessionState在web.config中正确配置,以使会话状态

  2. 确保使用Web服务有一个cookie的容器,其中ASP.NET会话cookie可以存储。如果客户是通过网络浏览器(例如AJAX调用),这通常开箱,但如果你正在建设一个独立的客户端,你必须做更多的工作,看上面的链接。



所有的一切:一个糟糕的设计决定给你超过必要的工作(对不起,擦吧)



我想你应该重新设计你的web服务,使您无论是总在所有方法发送的用户名和密码,或者添加,让客户与每个Web服务请求发送的令牌的登录方法。


I have two webmethod i have used session variable to exchange the username variable between two webmethods but its displaying null in second webmethod ,can we use cookie as alternate to session to store and retrieve username

[WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool SubmitList1(string username )
    {
       Session["User_Name"] = username;
      .........
        .......

     }
[WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public bool addresslisting( string keyword)
    {

       string username = Context.Session["User_Name"].ToString();
        ........
          .........
    }

解决方案

Disclaimer: I think that a web service that relies on Session state is just plain WRONG since a web service should be stateless. However:

At http://msdn.microsoft.com/en-us/library/aa480509.aspx you can read about how to use ASP.NET Session in a web service:

  1. Make sure that /configuration/system.web/sessionState in web.config is configured properly to enable session state
  2. Make sure that uses the web service has a cookie container where the ASP.NET session cookie can be stored. If the client is from a web browser (e.g. ajax call) this usually works out of the box, but if you are building a standalone client, you have to do some more work, see the link above.

All in all: a bad design decision gives you more work than necessary (sorry for rubbing it in).

I think you should redesign you web service so that you either always send username and password in all methods or add a login method that gives the client a token that is sent with each web service request.

这篇关于如何在asp.net web服务2的webMethods之间交换会话或Cookie变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆