【问题标题】:How to Call .NET AuthenticationService from json client without ASP.NET如何在没有 ASP.NET 的情况下从 json 客户端调用 .NET AuthenticationService
【发布时间】:2012-02-13 15:44:10
【问题描述】:

我有一个 WCF 4 服务,它位于 Secure 子文件夹中,可在客户端使用 .NET AuthenticationService 使用 Forms 身份验证进行身份验证后访问。

此 WCF 服务适用于通过 json 进行通信但不是 ASP.NET 应用程序的移动应用程序客户端。我已成功将服务配置为使用 json,并且 AuthenticationService 具有许多地方记录的标准配置,例如http://msdn.microsoft.com/en-us/library/bb398990.aspx

AuthenticationService 的文档说“应用程序必须能够发送和使用 SOAP 消息”。但是我希望客户端也能够使用 json 进行身份验证。这可能吗?需要什么配置?

我发现这篇文章http://weblogs.asp.net/asptest/archive/2008/12/09/working-with-the-asp-net-ajax-authentication-service.aspx 所以看起来 AuthenticationService 可以处理 json 但它使用客户端应用程序服务。移动应用客户端不是 ASP.NET 应用。

【问题讨论】:

    标签: wcf json forms-authentication


    【解决方案1】:

    是的,AuthenticationService 可以处理 JSON。有几种方法可以做到。这是我过去使用 元素的示例配置。

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    
        <services>
          <service name="System.Web.ApplicationServices.AuthenticationService" behaviorConfiguration="MyServiceBehavior">
            <endpoint address="" behaviorConfiguration="ajaxBehavior"
                      contract="System.Web.ApplicationServices.AuthenticationService"
                      binding="webHttpBinding" bindingConfiguration="webHttpBindingSecure"
                      bindingNamespace="http://asp.net/ApplicationServices/v200"/>
          </service>
        </services>
    
        <behaviors>
          <endpointBehaviors>
            <behavior name="ajaxBehavior">
              <enableWebScript/>
            </behavior>
    
          </endpointBehaviors>
          <serviceBehaviors>
    
            <behavior name="MyServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <bindings>
          <webHttpBinding>
            <binding name="webHttpBindingSecure">
              <security mode="Transport"/>
            </binding>
          </webHttpBinding>
        </bindings>
    </system.serviceModel>
    

    【讨论】:

      【解决方案2】:

      这个问题已经很久了,但我认为如果我发布我的答案会对某人有所帮助。

      当然,您可以为 AuthenticationService 返回 json。 解决方案非常简单,就像 Garret 回答一样,您只需要像这样配置另一个端点,但您需要为端点行为添加 2 个附加属性:defaultOutgoingResponseFormat="Json" 和 defaultBodyStyle="Wrapped" 以覆盖默认的肥皂响应。

      <system.serviceModel>
      <services>
        <service behaviorConfiguration="AuthenticationServiceBehaviors" name="System.Web.ApplicationServices.AuthenticationService">
          <endpoint address="" behaviorConfiguration="ajaxBehavior"
                     contract="System.Web.ApplicationServices.AuthenticationService"
                     binding="webHttpBinding" bindingConfiguration="RestBinding"
                     bindingNamespace="http://asp.net/ApplicationServices/v200"/>
        </service>
      </services>
      <bindings>
            <webHttpBinding>
          <binding name="RestBinding" />
        </webHttpBinding>
      </bindings>
      
      <behaviors>
        <endpointBehaviors>
          <behavior name="ajaxBehavior">
            <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped" />
          </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
          <behavior name="AuthenticationServiceBehaviors">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
      

      我希望这对想要将 asp.net 成员身份公开为 json 格式以在移动应用程序中使用的人有所帮助。

      【讨论】:

      • 这很有帮助。您是否将此与自动生成的代理(例如 authsvc.svc/js)或自定义 javascript 代码一起使用?请考虑发布您的客户端代码。
      • 嗨@BillVo 我正在使用它和自动生成的代理:authsvc/login ...当您将此端点公开为 JSON 时,您可以从使用 Javascript 的 Web 应用程序到移动应用程序的任何地方使用它。当您需要客户端的示例代码时,请参考 google,因为每个客户端堆栈都有不同的语法。问候。
      猜你喜欢
      • 1970-01-01
      • 2013-05-31
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      相关资源
      最近更新 更多