我相信这不是一个新的问题,但事实是,我一直无法让它工作。我有一个C# web表单应用程序,它可以验证用户,并在验证后创建一个cookie并调用WCF服务。
我对WCF相当陌生,所以任何帮助都将非常感激。
谢谢你的帮助。
Web表单应用程序。
if (validUser)
{
FormsAuthenticationTicket tkt;
string cookiestr;
HttpCookie ck;
tkt = new FormsAuthenticationTicket(1, TextBox1.Text, DateTime.Now, DateTime.Now.AddMinutes(30), chkPersistCookie, "sample data");
cookiestr = FormsAuthentication.Encrypt(tkt);
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
if (chkPersistCookie)
ck.Expires = tkt.Expiration;
ck.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(ck);
string strRedirect;
strRedirect = Request["ReturnUrl"];
if (strRedirect == null)
strRedirect = "default.aspx";
//Calling wcf service
ServiceReference1.Service1Client ServCli1 = new ServiceReference1.Service1Client();
ServCli1.GetData(12);
}
wcf服务。
public string GetData(int value)
{
string xpto = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Cookie];
var xpto1 = HttpContext.Current.Request;
return string.Format("You entered: {0}", value);
}
问题是WCF服务从来没有收到过cookie 我需要它,这样我就可以在WCF服务中验证用户。
web客户端web.config.: wcf web.config.: 问题是wcf服务从来没有收到过cookie,我需要在wcf服务里面验证用户。
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
<pages>
<namespaces>
<add namespace="System.Web.Optimization"/>
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
</controls>
</pages>
<authentication mode="Forms">
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:58515/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
wcf web.config:
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
<authentication mode="Forms">
</authentication>
</system.web>
<system.serviceModel>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
您可以使用OperationContextScope在客户端应用程序中创建一个新的上下文,以向发出的消息添加自定义头。你可以使用这个方法来传递cookie到服务器端。这是我的演示。
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add(HttpRequestHeader.Cookie, "<Test Cookie>");
using (OperationContextScope scope = new OperationContextScope(service1Client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
service1Client.YZ();
}
这是客户端的代码。使用OperationContextScope将cookie传递给服务器端。
public void YZ()
{
string xpto = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Cookie];
Console.WriteLine(xpto);
}
这是服务器端的代码。执行成功后,会输出cookie。
关于OperationContextScope的详细信息,请参考以下链接。