如何将 Cookie 从网络应用程序发送到 wcf 服务?



我确定这不是一个新问题,但事实是我无法让它工作。 我有一个 C# Web 窗体应用程序,该应用程序对用户进行身份验证,并在验证后创建一个 cookie 并调用 WCF 服务。

我是WCF的新手,所以任何帮助将不胜感激。

谢谢。

网络表单应用:

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:

<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的更多信息,请参阅以下链接:

https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontextscope?view=dotnet-plat-ext-3.1

最新更新