【问题标题】:How do i send a cookie from web app to wcf service?如何将 cookie 从 Web 应用程序发送到 wcf 服务?
【发布时间】:2020-09-26 08:45:40
【问题描述】:

我确定这不是一个新问题,但事实是我无法让它发挥作用。 我有一个 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.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>

【问题讨论】:

    标签: wcf-security wcf-client


    【解决方案1】:

    您可以使用 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://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontextscope?view=dotnet-plat-ext-3.1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多