【问题标题】:Get Windows username via WCF Data Services (Silverlight)通过 WCF 数据服务 (Silverlight) 获取 Windows 用户名
【发布时间】:2013-02-12 04:01:53
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • 这对我不起作用,因为我不是在寻找身份验证。我只需要 Windows 用户名,我还查看了其他帖子。我将电流设为空。

标签: windows wcf


【解决方案1】:

我尝试创建一个演示应用程序来测试事物,以下对我有用..

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

希望这会有所帮助...

【讨论】:

  • 我试过了,它要求在网络应用程序中输入用户名和密码。我也尝试在身份验证方法中禁用匿名访问。
  • 非常感谢您的尝试。
  • 您对 Web 应用使用什么身份验证? Asp Net 表单认证?你的 web 应用程序也必须使用 NTLM,否则你怎么知道哪个 windows 用户正在登录?
  • 您必须启用模拟。检查here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
相关资源
最近更新 更多