2012-01-18 43 views
3

我有一個位於Secure子文件夾中的WCF 4服務,可以在客戶端使用.NET AuthenticationService使用Forms身份驗證進行身份驗證後訪問。如何在不使用ASP.NET的情況下從json客戶端調用.NET AuthenticationService

此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應用程序。

回答

0

是的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> 
0

這個問題很久沒有解決,但我認爲如果我發佈我的答案,它會幫助別人。

當然,您可以返回AuthenticationService的json。 解決方案非常簡單,就像Garret的答案一樣,您只需要配置另一個端點,但您需要爲端點行爲添加2個附加屬性:defaultOutgoingResponseFormat =「Json」和defaultBodyStyle =「Wrapped」以覆蓋默認的soap響應。

<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格式的移動應用程序使用。

+0

這是有幫助的。您是否使用了自動生成的代理(例如authsvc.svc/js)或自定義JavaScript代碼?請考慮發佈您的客戶端代碼。 – BillVo 2015-07-07 14:32:21

+0

嗨@BillVo我使用這個自動生成的代理服務器:authsvc/login ...當你已經暴露了這個enpoint作爲JSON,你可以從任何地方使用Javascript到移動應用程序的Web應用程序。請在客戶端需要示例代碼時參考google,因爲每個客戶端堆棧都有不同的語法。問候。 – tungnt185 2015-07-08 03:40:55

相關問題