0

我正在尝试使用 WCF 数据服务为我的 silverlight 4 应用程序获取 Windows 用户名。当我从本地计算机调试应用程序时,我看到了用户名。但是当我从 Web 服务器运行应用程序时,用户名显示为空。如果您需要任何其他详细信息,请告诉我。任何帮助是极大的赞赏。

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

[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>
4

1 回答 1

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>

希望这可以帮助...

于 2013-02-11T20:01:53.270 回答