具有表单身份验证的WCF

具有表单身份验证的WCF,wcf,forms-authentication,httpcontext,wcf-rest,dotnet-httpclient,Wcf,Forms Authentication,Httpcontext,Wcf Rest,Dotnet Httpclient,我有一个带有WCF REST服务的asp.net 4.5 webforms项目。aspx页面使用HttpClient从代码隐藏中调用这些服务。所有的get和post服务都很好 我们最近在整个解决方案中添加了表单身份验证,当我尝试访问任何页面或WCF服务(例如:localhost/service/Test.svc)时,它会重定向到登录页面。当我登录并尝试使用HttpClient从aspx页面的代码隐藏中点击服务时,它不会在WCF上获取我的HttpContext,而是返回登录页面的html。我已将以

我有一个带有WCF REST服务的asp.net 4.5 webforms项目。aspx页面使用HttpClient从代码隐藏中调用这些服务。所有的get和post服务都很好

我们最近在整个解决方案中添加了表单身份验证,当我尝试访问任何页面或WCF服务(例如:localhost/service/Test.svc)时,它会重定向到登录页面。当我登录并尝试使用HttpClient从aspx页面的代码隐藏中点击服务时,它不会在WCF上获取我的HttpContext,而是返回登录页面的html。我已将以下内容添加到web.config中的WCF设置中:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
并尝试使用WebRequestHandler()处理程序将所有Cookie从HttpContext传递到HttpClient实例:

var handler=new WebRequestHandler();
CookieContainer cc=新CookieContainer();
for(int i=0;i
web.config:

<authentication mode="Forms"> 
  <forms defaultUrl="~/" loginUrl="~/Account/Login"></forms> 
</authentication> 
<authorization> 
  <deny users="?"/> 
</authorization> 
</system.web> 
<location path="Service/TestService.svc"> 
 <system.web> 
  <authorization> 
   <allow users="*"/> 
  </authorization> 
 </system.web> 
</location>

我是否在代码中做了一些错误的事情,或者WCF服务不可能与客户机在同一个项目中并使用HttpContext?如果需要更多代码,请告诉我。我不确定我应该提供什么,因为没有那么复杂


-谢谢

我们可以查看您的WCF方法代码和
system.web/authentication
部分
web.config
?另外,您说.aspx调用WCF-它们是在不同的服务器上还是在IIS实例上?web.config:我认为web.config的格式不正确。但我希望你能得到这份工作。wcf和aspx位于同一个IIS实例、同一个网站上。您在同一实例上进行
.aspx
页面调用
wcf
的原因是什么?我的意思是,如果您想将这两种方法都公开给客户机,那么为什么不从这两个地方调用通用方法呢?我们还没有决定是否将服务移出。从技术上讲,我可以直接从aspx访问数据库。将来,我们可能会托管服务,但希望现在就构建基础设施。我想用WCF实现的目标是不可能的。这绝对是可能的。您是否可以尝试使用
WebClient
而不是
HttpClient
发出请求。看见
var handler = new WebRequestHandler();
                CookieContainer cc = new CookieContainer();
                for (int i = 0; i < Request.Cookies.Count; i++)
                {
                    Cookie oC = new Cookie();
                 // Convert between the System.Net.Cookie to a System.Web.HttpCookie...
                    oC.Domain   = "ss";
                    oC.Expires  = Request.Cookies[i].Expires;
                    oC.Name     = Request.Cookies[i].Name;
                    oC.Path     = Request.Cookies[i].Path;
                    oC.Secure   = Request.Cookies[i].Secure;
                    oC.Value    = Request.Cookies[i].Value;
                    cc.Add(oC);
                }
                handler.CookieContainer = cc;
                handler.UseDefaultCredentials = true;
                handler.AllowPipelining = true;
                handler.UseCookies = true;                
                using (var client = new HttpClient(handler)){}
<authentication mode="Forms"> 
  <forms defaultUrl="~/" loginUrl="~/Account/Login"></forms> 
</authentication> 
<authorization> 
  <deny users="?"/> 
</authorization> 
</system.web> 
<location path="Service/TestService.svc"> 
 <system.web> 
  <authorization> 
   <allow users="*"/> 
  </authorization> 
 </system.web> 
</location>