2013-02-11 52 views
0

我正在嘗試使用WCF數據服務獲取我的Silverlight 4應用程序的Windows用戶名。我在從本地計算機調試應用程序時看到了用戶名。但是,當我從Web服務器運行應用程序時,用戶名會以空值出現。 請讓我知道你是否需要任何其他細節。任何幫助是極大的讚賞。通過WCF數據服務獲取Windows用戶名(Silverlight)

這裏是我使用來獲取用戶名的方法:

[OperationContract] 
public string GetWindowsUsername() 
{ 
    string usr = HttpContext.Current.User.Identity.Name; 
    return usr.ToString(); 
} 

這裏是我的web.config:

<system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
     <httpRuntime maxRequestLength="2147483647" /> 
     <authentication mode="Windows" /> 
     <!--<anonymousIdentification enabled="true"/>--> 
     <authorization> 
     <allow users="*"/> 
     </authorization> 
     <!--<identity impersonate="false" />--> 
    </system.web> 
<system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name=""> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
        <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="OrderTrackingSystem.Web.OTSService.customBinding0" 
        maxBufferPoolSize="2147483647" maxBufferSize="2147483647" 
        maxReceivedMessageSize="2147483647" transferMode="Streamed"> 
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
         maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
        <security> 
        <transport clientCredentialType="Ntlm" /> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
     <services> 
      <service name="OrderTrackingSystem.Web.OTSService"> 
       <endpoint address="" binding="basicHttpBinding" bindingConfiguration="OrderTrackingSystem.Web.OTSService.customBinding0" 
        name="OTSServiceCustom" contract="OrderTrackingSystem.Web.OTSService" /> 
      </service> 
     </services> 
    </system.serviceModel> 

這裏是我的服務配置:

<configuration> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="OTSServiceCustom" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" /> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:49458/OTSService.svc" binding="basicHttpBinding" 
     bindingConfiguration="OTSServiceCustom" contract="OTSProxy.OTSService" 
     name="OTSServiceCustom" /> 
    </client> 
    </system.serviceModel> 
</configuration> 
+1

檢查此http://stackoverflow.com/q/292233/860243 – Flowerking 2013-02-11 16:52:41

+0

這不適合我,因爲我不尋找認證。我只需要Windows用戶名,我也看了另一篇文章。我得到當前爲空。 – user2062004 2013-02-11 17:43:48

回答

0

我試圖創建一個演示應用程序測試的東西,以下爲我工作..

[OperationContract] 
public string GetWindowsUsername() 
{ 
    string usr = ServiceSecurityContext.Current.WindowsIdentity.Name; 
    return usr; 
} 

但序使這項工作,你必須配置爲綁定:

<bindings> 
    <basicHttpBinding> 
     <binding name="OTSServiceCustom"> 
      <security mode="TransportCredentialOnly"> 
       <transport clientCredentialType="Ntlm" proxyCredentialType="None" /> 
      </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

希望這幫助...

+0

我試過了,它要求在Web應用程序中輸入用戶名和密碼。我通過在身份驗證方法中禁用匿名訪問來嘗試它。 – user2062004 2013-02-12 21:15:44

+0

非常感謝您嘗試。 – user2062004 2013-02-12 21:16:24

+0

您使用什麼身份驗證爲Web應用程序? Asp Net表單身份驗證?您還必須爲您的網絡應用程序使用NTLM,否則您如何知道Windows用戶正在登錄? – Flowerking 2013-02-12 21:40:43

相關問題