2010-01-18 76 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服务的应用程序池!不建议在生产中这样做,但如果它起作用,至少你有一个起点。祝你好运。

相关问题