通过WCF数据服务获取Windows用户名(Silverlight)

问题描述:

我正在尝试使用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://*.com/q/292233/860243 – Flowerking 2013-02-11 16:52:41

+0

这不适合我,因为我不寻找认证。我只需要Windows用户名,我也看了另一篇文章。我得到当前为空。 – user2062004 2013-02-11 17:43:48

我试图创建一个演示应用程序测试的东西,以下为我工作..

[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