如何在asp.net webservices的两个webmethod之间交换会话或cookie变量 [英] how to exchange session or cookie variables between two webmethods in asp.net webservices

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

问题描述

我有两个webmethod我使用会话变量交换两个webmethod之间的用户名变量,但它显示null在第二个webmethod,我们可以使用cookie作为替代会话存储和检索用户名

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();
        ........
          .........
    }


推荐答案

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

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

http://msdn.microsoft.com/en-us/library/aa480509.aspx 您可以阅读有关如何在Web服务中使用ASP.NET会话:

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. 确保正确配置了web.config中的/configuration/system.web/sessionState以启用会话状态

  2. 确保使用Web服务有一个cookie容器,可以存储ASP.NET会话cookie。如果客户端是从Web浏览器(例如ajax调用),这通常是开箱即用,但如果你正在构建一个独立的客户端,你必须做一些更多的工作,请参阅上面的链接。

总之:不好的设计决定给你更多的工作(对不起,揉搓它)。

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

我认为你应该重新设计你的web服务,以便你总是在所有方法中发送用户名和密码,或者添加一个登录方法,给客户端一个令牌,每个web服务请求发送。

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 webservices的两个webmethod之间交换会话或cookie变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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