2010-01-18 75 views
0

我正在使用asmx Web服務來鎖定遠程計算機上的文件夾!在遠程計算機上執行Web服務

當我運行在本地機器上的一切web服務工作正常,但是當我在遠程計算機沒有運行這一點,文件夾,遠程計算機逗留解鎖!

我supose,我需要設置安全權限的遠程計算機上的該Web服務,但我不知道在哪裏!

那麼,我需要什麼來啓用遠程計算機上執行此服務?

+4

無關注:在句子末尾學習一段時間的價值。 – Chris 2010-01-21 00:53:01

回答

0

我懷疑這是權限,沒有網絡服務已讀/寫訪問到文件夾?

也許你可以嘗試身份模仿。

<system.web> 
<identity impersonate="true" userName="WindowsDomain\YourUserName" password="YourPassword" /> 
</system.web> 

編輯我會通過檢查服務器上的文件夾對網絡服務的寫權限開始。如果文件夾安全性無法更改,請使用Web配置中的身份模擬並將其映射到服務器上的用戶。

EDit 2當代碼嘗試鎖定文件夾時,是否會遇到任何類型的錯誤?

+0

感謝您的回覆。我如何設置此Web服務具有讀/寫權限? – Comii 2010-01-18 09:09:00

+0

它不是需要權限的Web服務設置服務器上的文件夾權限。您需要在服務器上有RDP/VNC訪問權限,右鍵單擊文件夾並授予網絡權限讀/寫權限。 – Rippo 2010-01-18 09:10:33

0

在遠程asmx下運行的憑據是什麼?它是否有權在自己的文件夾結構之外對文件系統執行操作?

0

這是刪除用戶的功能,允許在某個文件夾的權限:

Public Function RemoveAllowPermission(ByVal filePath As String, ByVal username As String, ByVal power As String) 

     Dim dirinfo As DirectoryInfo = New DirectoryInfo(filePath) 

     Dim dirsecurity As DirectorySecurity = dirinfo.GetAccessControl() 
     dirsecurity.SetAccessRuleProtection(True, True) 
     Select Case power 

      Case "FullControl" 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)) 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)) 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)) 

      Case "ReadOnly" 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow)) 

      Case "Write" 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)) 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)) 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)) 

      Case "Modify" 

       dirsecurity.RemoveAccessRuleAll(New FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow)) 

     End Select 

     dirinfo.SetAccessControl(dirsecurity) 

    End function 

在接下來的功能我叫RemoveAllowPermission功能:

<WebMethod()> _ 
    Public Function ChangePermission() 
     Dim file As String = "C:\Pictures" 
     Dim fs As FileSecurity = System.IO.File.GetAccessControl(file) 
     Dim owner As NTAccount = CType(fs.GetOwner(GetType(NTAccount)), NTAccount) 

     Dim usergroup As AuthorizationRuleCollection = fs.GetAccessRules(True, True, (GetType(System.Security.Principal.NTAccount))) 
     Try 
      For Each Rule As FileSystemAccessRule In usergroup 
       RemoveAllowPermission(file, Rule.IdentityReference.Value, "FullControl") 
       Next 
     Catch ex As Exception 
Return ("Error") 
     End Try 
    End Sub 
Return 0 
End Class 

所以,當我遠程計算機我ChangePermission功能上運行的服務捕獲異常並返回異常消息錯誤!

+0

請使用此信息更新您的問題。您已添加答案,但信息屬於該問題。 – 2010-01-18 19:39:42

0

因爲它是ASMX,我認爲它屬於對ASP.NET的模擬規則。由於沒有編程的登錄功能,你應該使用非託管API。

比方說,你需要做的事情中(有你想去的地方訪問遠程計算機用戶帳戶下)的模擬環境。

Impersonation.Execute(myEntity.NasUser, myEntity.NasPassword,() =>  
{  
//Copy File to UNC Path for example 
    File.Copy(sourceFile, Path.Combine(myEntity.UploadPath, Path.GetFileName(sourceFile)), true);  
}); 

導入非託管API:

[DllImport("advapi32.dll", SetLastError = true)]  
    public static extern bool LogonUser(  
     string lpszUsername,  
     string lpszDomain,  
     string lpszPassword,  
     int dwLogonType,  
     int dwLogonProvider,  
     out IntPtr phToken  
     );  
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]  
    public extern static bool CloseHandle(IntPtr handle);  

上述執行可能是類似的東西:

public static void Execute(string userName, string domain, string password, Action action)  
    {  
     try  
     {  
      bool bImpersonated = LogonUser(  
       userName,  
       domain,  
       password,  
       logon32LogonInteractive,  
       logon32ProviderDefault,  
       out tokenHandle);  
      if (bImpersonated == false)  
      {  
       throw new Win32Exception(Marshal.GetLastWin32Error());  
      }  
      WindowsIdentity newId = new WindowsIdentity(tokenHandle);  
      impersonatedUser = newId.Impersonate();  
      action();  
     }  
     catch (Exception ex)  
     {  
      throw ex;  
     }  
     finally  
     {  
      if (impersonation != null)  
       impersonation.Dispose();  
     }  
    } 

你不應該忘記撤消模擬,並返回到以前的windowscredentials狀態:

public void Dispose()  
{  
    // Stop impersonating the user.  
    if (impersonatedUser != null)  
     impersonatedUser.Undo();  
    // close handle  
    if (tokenHandle != IntPtr.Zero)  
     CloseHandle(tokenHandle);  
} 
0

那麼你可以像管理員帳號一樣運行Web服務的應用程序池!不建議在生產中這樣做,但如果它起作用,至少你有一個起點。祝你好運。

相關問題