惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LangChain Blog
WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
The Cloudflare Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler
小众软件
小众软件
量子位
月光博客
月光博客
P
Proofpoint News Feed
IT之家
IT之家
腾讯CDC
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
雷峰网
雷峰网
V
Visual Studio Blog

博客园 - BigRain

小学生学习汉字,汉字抓取 c# regex regextools cache 访问频率的思考 python 百度cpc点击 20130118SQL记录 PPT辅助工具 检查外链的方法 百度调价HttpWebRequest 装饰者模式 第一个android程序闪亮登场 SQL 的一些批量插入和修改 职责链模式的运用 State Pattern 程序人生 SerialPort实现modem的来电显示 利用枚举进行状态的设计 singleton模式 在软件开发中的运用 我对当前项目的一些看法 闲话闲说——关于异常
webService启用cookie的另一种方法
BigRain · 2012-06-14 · via 博客园 - BigRain

很多时候,我们希望用到asp.net的membership,这样就可以快速的处理角色验证之类的问题。而且客服端能够与bs系统共用一套账户。

webservice里面的proxy类继承自WebClientProtocol,而在WebClientProtocol里面包含两个方法

protected virtual WebRequest GetWebRequest(Uri uri);

protected virtual WebResponse GetWebResponse(WebRequest request);

我们应该很清楚,调用webservice本质上也是一个http的Post请求,和普通的html不一样的是,webservice请求的内容和服务返回的内容不是html,而是xml(soap协议)。再看这两个方法,所以proxy在调用service的时候,肯定会调用到这两个方法。所以,我们只要进行适当的重写这两个方法就OK了。

private static string currentCookie = string.Empty;
        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest request = base.GetWebRequest(uri);
            if (!string.IsNullOrEmpty(currentCookie))
            {
                request.Headers[HttpRequestHeader.Cookie] = currentCookie;
            }
            return request;
        }
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            if (!string.IsNullOrEmpty(response.Headers[HttpResponseHeader.SetCookie]))
                currentCookie = response.Headers[HttpResponseHeader.SetCookie];
            return response;
        }

我们直接从http请求的header里面去截取cookie,在放送之前,在Request的header上附加上cookie。

服务端,web.config需要配置一下,

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

启用所有module通道,这样就会进行权限验证。