2011-08-02 66 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中設置正確的安全組。

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