调用WCF webservice时清空HttpContext

前端之家收集整理的这篇文章主要介绍了调用WCF webservice时清空HttpContext前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近编写了一个与Silverlight一起使用的Web服务,它使用ASP.net成员资格和角色.

要验证服务中的客户端,我查看HTTPContext.Current.User(当从Silverlight调用服务时,它可以工作)

但是,我一直试图从asp.net回发中调用相同的服务.但是,当我逐步访问服务时,HTTPContext.Current有一个用户名的emplty字符串.

我猜我有没有在web.config文件中做的事情导致httpContext没有通过代理发送到我的服务?

任何想法,将不胜感激.我需要能够以某种方式验证客户端使用asp.net成员资格和角色,并让它从asp.net客户端和Silverlight客户端工作.

解决方法

解决了!

默认情况下,Silverlight应用程序将所有浏览器cookie发送到服务.其中一个cookie是“.ASPXAUTH”cookie,用于对成员资格和角色进行身份验证.

然而,asp.net应用程序没有将cookie发送到服务.要发送授权cookie,我在调用webservice方法之前使用了以下代码.

using (OperationContextScope scope = new OperationContextScope(ws.InnerChannel))
    {
HttpRequestMessageProperty httpRequest = new HttpRequestMessageProperty();
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name,httpRequest);

            HttpCookieCollection cc = Page.Request.Cookies;
            if (Request.Cookies[".ASPXAUTH"] != null)
            {
                HttpCookie aCookie = Request.Cookies[".ASPXAUTH"];
                String authcookieValue = Server.HtmlEncode(aCookie.Value);
                httpRequest.Headers.Add("Cookie: " + ".ASPXAUTH=" + authcookieValue);

            }
// Webservice call goes here
    }

猜你在找的WebService相关文章