4

我試圖在Intranet ASP.Net Web應用程序中使用模擬和委派,以便將已通過身份驗證的用戶憑據傳遞到文件服務器所以它可以將文件寫入目錄。嘗試使用模擬和委派連接到文件服務器的ASP.Net Web應用程序

Web服務器和文件服務器是兩臺獨立的機器,但在同一個域中,因此需要Delegation。

我已經做了以下內容:

  • 在我的web應用程序的web.config設置<authentication mode="Windows"/> and <identity impersonate="true"/>
  • 已啓用約束 代表團從Web服務器到文件服務器的主機服務和 CIFS(通用互聯網文件系統,Active Directory中。
  • 啓用僅 Windows身份驗證的網站,通過IIS。

顯然這應該都可以工作,但並不是這樣,當我嘗試從Web應用程序在文件服務器上創建一個文件夾時,我遇到了ACCESS DENIED

我讀過的所有網頁似乎都表明我的設置應該工作,我是什麼樣的missi NG?

注:

  • 我的用戶名被傳遞到Web服務器的罰款。
  • 我有充分的權利,我在創建該文件夾的文件夾一組 的一部分。
+0

您是否嘗試過直接寫入文件而不通過應用程序?同時獲取失敗操作的進程監視器輸出可能會有所幫助 – Mike

+0

直接編寫文件可以起作用。 –

回答

0

我們的ASP.NET項目使用單獨的文件服務器上。我們發現一個可靠的方法是通過WNetAddConnection2A API調用打開輔助連接。這是一個基本的實現(VB.NET)

API聲明

<StructLayout(LayoutKind.Sequential)> Private Structure NETRESOURCE 
    Public dwScope As Integer 
    Public dwType As Integer 
    Public dwDisplayType As Integer 
    Public dwUsage As Integer 
    <MarshalAs(UnmanagedType.LPStr)> Public lpLocalName As String 
    <MarshalAs(UnmanagedType.LPStr)> Public lpRemoteName As String 
    <MarshalAs(UnmanagedType.LPStr)> Public lpComment As String 
    <MarshalAs(UnmanagedType.LPStr)> Public lpProvider As String 
End Structure 

<DllImport("mpr.dll")> _ 
Private Shared Function WNetAddConnection2A(_ 
<MarshalAs(UnmanagedType.LPArray)> ByVal lpNetResource As NETRESOURCE(), _ 
<MarshalAs(UnmanagedType.LPStr)> ByVal lpPassword As String, _ 
<MarshalAs(UnmanagedType.LPStr)> ByVal lpUserName As String, _ 
ByVal dwFlags As Integer) As Integer 

End Function 

<DllImport("mpr.dll")> _ 
Private Shared Function WNetCancelConnection2A(_ 
<MarshalAs(UnmanagedType.LPStr)> ByVal lpName As String, _ 
ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer 

End Function 


Public Shared Sub WNetAddConnection2AEx(ByVal i_sPath As String, ByVal i_sPassword As String, ByVal i_sUserID As String) 
    Dim nr(1) As NETRESOURCE 
    nr(0).lpRemoteName = i_sPath 
    nr(0).lpLocalName = "" 
    nr(0).dwType = 1 
    nr(0).dwDisplayType = 0 
    nr(0).dwScope = 0 
    nr(0).dwUsage = 0 
    nr(0).lpComment = "" 
    nr(0).lpProvider = "" 
    Dim iErr As Integer = WNetAddConnection2A(nr, i_sPassword, i_sUserID, 0) 
    If iErr > 0 Then Throw New Exception("Can not connect to share folder: " & i_sPath) 
End Sub 

使用

WNetAddConnection2AEx("\\server\path", "password", "user_id") 
''... 
''perform your file operation here 
''... 
WNetCancelConnection2A("\\server\path", 0, -1) 
0

配置IIS代表團 要打開集成Windows身份驗證和模擬的一個ASP.NET連接的應用程序,您必須配置Internet信息服務冰(IIS)。要在IIS中配置Windows身份驗證,請按照下列步驟操作: 單擊開始,單擊運行,鍵入inetmgr,然後單擊確定。 展開本地計算機,然後展開網站。 用鼠標右鍵單擊默認網站,然後單擊屬性。 單擊目錄安全性選項卡,然後單擊匿名訪問和身份驗證控制下的編輯。 單擊以選中「集成Windows身份驗證」複選框,然後單擊以清除「匿名訪問」,「Windows域服務器的摘要式身份驗證」和「基本身份驗證」複選框。

1

很有可能您遇到了使用Kerberos身份驗證的double hop問題。有兩個我知道的選項。

  1. 設置您的SPN爲您的網站的身份。
  2. 關閉的Kerberos在IIS和使用NTLM唯一的身份驗證。

我認爲最好的選擇是選項1,但這取決於您在網絡上擁有多少訪問權限。

這裏的KB因爲它涉及到IIS6。

也發表您的應用程序池的配置方式。

+0

我正在使用IIS 6.所以我不認爲Kerberos在IIS中打開。 –

+0

同樣適用於IIS 6.讓我找到一些IIS 6鏈接。 –

0

好吧,我知道了!我有一個ASPX頁面來讀取文件,寫入文件,並在另一臺服務器的UNC路徑上創建文件夾。這是在兩個Windows Server 2012實例上完成的,運行.Net Framework 4,使用通用工作組類型網絡,沒有域身份驗證。

這裏是什麼了......

  1. 設置兩個服務器上的用戶「文件共享」,用相同的密碼。
  2. 將「FileShare」添加到IIS服務器上的「IIS_IUSRS」組,否則將無法編譯該站點
  3. 在目標服務器的物理文件夾上爲「FileShare」授予讀/寫/修改權限
  4. 共享文件夾說在網絡上\\ MyDestServer \ MySharedFolder
  5. 的Web.config <authentication mode="Windows" />
  6. 的Web.config <identity impersonate="true" userName="FileShare" password="password"/>

現在用你的代碼寫一個文件到網絡文件夾,這裏的我用過的一個樣本。

try 
    { 
     string strFile = "\\MYDestServer\MySharedFolder\Test.txt"; 
     if (!System.IO.File.Exists(strFile)) 
     { 
      var stream = System.IO.File.CreateText(strFile); 
      stream.WriteLine("This file was created on: " + DateTime.Now.ToString()); 
      stream.Close(); 
      stream.Dispose(); 
      litCreateFileTest.Text = "File Created<br/>" + strFile; 
     } 
     else 
     { 
      var inStream = System.IO.File.OpenText(strFile); 
      string strContent = inStream.ReadToEnd(); 
      inStream.Close(); 
      inStream.Dispose(); 

      strContent += "modified on: " + DateTime.Now.ToString() + "\r\n"; 

      var outStream = System.IO.File.CreateText(strFile); 
      outStream.Write(strContent); 
      outStream.Close(); 
      outStream.Dispose(); 

      litCreateFileTest.Text = "File Updated<br/>" + strFile; 
     } 
    } 
    catch (Exception ex) 
    { 
     litCreateFileTest.Text = "Error: " + ex.Message; 
    } 
0

我們去了Active Directory用戶和組。

  1. 找到Web服務器,右鍵單擊並選擇屬性。
  2. 在委派選項卡上,我們爲任何協議啓用委派。

這並沒有馬上工作,但我們認爲這是什麼最終做到了。

我將進一步測試驗證當我們移動應用給我們的舞臺環境。

相關問題