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

时间:2013-02-11 16:43:47

标签: windows wcf username

我正在尝试使用WCF数据服务获取Silverlight 4应用程序的Windows用户名。当我从本地计算机调试应用程序时,我看到了用户名。但是,当我从Web服务器运行应用程序时,用户名将显示为null。 如果您需要任何其他细节,请告诉我。非常感谢任何帮助。

以下是我用来获取用户名的方法:

[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 个答案:

答案 0 :(得分: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>

希望这会有所帮助......