2011-08-02 63 views
0

我想获取正在访问我的本地IIS上承载的ASP.NET页面的客户端的Windows用户名。我在ASP.NET页面中调用了一个WCF服务,该页面返回客户端的Windows用户名。我碰到就那么多帖子,其中大部分都表明尝试获取使用WCF客户端的Windows用户名

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

应该工作。我面临的问题是“1”总是返回空值。 “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> 
+0

停下来告诉你要做什么? –

+0

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

回答

0

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

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文件):

 
<authentication mode="Windows"/> 
<identity impersonate="true"/> 

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

+0

那么我该如何解决这个问题。你可以让我知道。我是WCF的新手。感谢您的帮助。谢谢。 – Santosh

+0

所以显而易见的答案(而不仅仅是观察)是为了启用模拟。看看这里:http://geekswithblogs.net/manesh/archive/2009/04/23/setting-up-wcf-to-impersonate-client-credentials.aspx,http://geekswithblogs.net/robz/archive /2007/10/03/wcf-impersonation---specifying-windows-authentication-credentials-on-the-service.aspx –

+0

我尝试了您提到的更改。它力求解决。它仍然是一样的。 :( – Santosh

0

我们的企业希望其网站只打开那些使用公司 设备(笔记本电脑)。所以他们希望SharePoint站点使用Windows 身份验证,而不是提示用户名和密码。如果 窗口用户名与已批准用户列表中的用户名匹配,则其 应该打开,否则将重定向到不同的验证页。得到 第一部分完成了一直是一个问题。

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

你所描述的不是安全性,它永远不会工作。