【问题标题】:Trying to obtain windows username of the client using WCF尝试使用 WCF 获取客户端的 Windows 用户名
【发布时间】:2011-08-02 14:34:09
【问题描述】:

我正在尝试获取正在访问托管在我的本地 IIS 上的 ASP.NET 页面的客户端的 Windows 用户名。我在 ASP.NET 页面中调用 WCF 服务,它返回客户端的 Windows 用户名。我遇到了很多有关的帖子,其中大多数都建议

  1. OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name
  2. HttpContext.Current.User.Identity.Name
  3. HttpContext.Current.User.Identity.Name

应该可以。我面临的问题是“1”总是返回 Null。 “2”和“3”总是返回我的本地用户名,而不是请求用户名。我是否在 ASP.NET 和 WCF 服务的 web.configs 中遗漏了任何内容。

IIS 属性:启用集成 Windows 身份验证。

这里是代码。

WCF

public string GetWindowsUser()    
{
   string temp = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
   string temp1 = HttpContext.Current.User.Identity.Name;
   string temp2 = HttpContext.Current.User.Identity.Name;
   return "Temp: "+temp+" \nTemp1: "+temp1+" \nTemp2: "+temp2;
}

WEB.Config

<system.web>
    <compilation debug="false" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:4772/Services.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="WindowsAuthServices.IService1" name="BasicHttpBinding_IService1"/>
    </client>
</system.serviceModel>

ASP.NET 页面:

protected void Page_Load(object sender, EventArgs e)
{
    WindowsAuthServices.Service1Client client = new WindowsAuthServices.Service1Client();
    lblWelcome.Text = client.GetWindowsUser();
}

Web.Config

<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:4772/Services.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="WindowsAuthServices.IService1" name="BasicHttpBinding_IService1"/>
    </client>
</system.serviceModel>

【问题讨论】:

  • 停下来告诉你要做什么?
  • 我们的企业希望它的网站只打开那些使用公司设备(笔记本电脑)的网站。所以他们希望共享点站点使用 Windows 身份验证,而不是提示用户名和密码。如果 Windows 用户名与已批准用户列表中的用户名匹配,则应打开或重定向到不同的身份验证页面。完成第一部分一直是个问题。

标签: windows wcf authentication


【解决方案1】:

这是因为您的调用是以运行 ASP.Net 工作进程的身份而不是以请求页面的用户身份(称为模拟)的身份进行的。

来自http://geekswithblogs.net/robz/archive/2007/10/03/wcf-impersonation---specifying-windows-authentication-credentials-on-the-service.aspx

1) ASP.NET 客户端 web.config 文件需要使用以下标记设置模拟(我将其放在身份验证元素下,如图所示):

2) 必须将服务行为配置为使用 Windows 获取权限并模拟调用者。

行为> 服务行为>

【讨论】:

【解决方案2】:

我们的企业希望它的网站只开放那些使用公司的人 设备(笔记本电脑)。所以他们希望 sharepoint 站点使用 Windows 身份验证而不提示用户名和密码。如果 windows 用户名与已批准用户列表中的用户名匹配 应该打开或重定向到不同的身份验证页面。得到 完成的第一部分有问题。

因此,您只需将笔记本电脑连接到 windows 域并使用集成安全并在 Sharepoint 中设置正确的安全组。

您所描述的不是安全性,它永远不会起作用。

【讨论】:

    【解决方案3】:

    听起来您需要为identity delegation. 实施/配置您的网站如果您需要为委派配置 WCF,请查看this MSDN article.

    【讨论】:

      【解决方案4】:

      我终于找到了解决办法。当其他用户尝试使用 IP 访问我的本地 IIS 时,我的本地 IIS 假定它是 Internet 请求而不是 Intranet 请求,并且由于其 Windows 身份验证,它仅适用于 Intranet 请求。为了解决这个问题,我不得不在我们的一台域服务器上托管我的网站。由于域服务器已经设置为只有该域中的用户才能访问它,所以现在它具有 Windows 登录信息。我的痛苦就这样结束了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多