【问题标题】:Copying file to shared drive fails in ASP.net c#在 ASP.net c# 中将文件复制到共享驱动器失败
【发布时间】:2014-02-14 17:34:39
【问题描述】:

我有 ASP.Net 和 C# 应用程序。我正在将图像上传到站点并将它们存储在C:\Images 目录中,它工作正常。当我将图像保存到 C:\Images 文件夹并同时复制(或有时移动)到 shared 驱动器时,我使用 shared 驱动器物理地址,它看起来像 @ 987654323@。此驱动器映射到部署服务器。我正在为该站点使用 IIS 托管。

问题在于 共享 驱动器复制。当我从本地计算机(部署站点的位置)使用站点时,将文件复制到 shared 驱动器。但是,当我从另一台机器(部署的服务器除外)使用该站点时,该站点将图像保存在C:\Images,但它不会将文件复制到共享驱动器。

这是我正在使用的代码

**Loggedon 方法调试成功。

  public static void CopytoNetwork(String Filename)
    {
        try
        {

            string updir = System.Configuration.ConfigurationManager.AppSettings["PhysicalPath"].ToString();

            WindowsImpersonationContext impersonationContext = null;
            IntPtr userHandle = IntPtr.Zero;
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;
            String UserName = System.Configuration.ConfigurationManager.AppSettings["Server_UserName"].ToString();
            String Password = System.Configuration.ConfigurationManager.AppSettings["server_Password"].ToString();
            String DomainName = System.Configuration.ConfigurationManager.AppSettings["Server_Domain"].ToString();


            bool loggedOn = LogonUser(UserName, DomainName, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref userHandle); 
            try
            {
                File.Move(@"C:\Images\" + Filename, updir + "\\" + Filename);
             }
            catch (Exception)
            {
            }
            finally
            {
                if (impersonationContext != null)
                {
                    impersonationContext.Undo();
                }

                if (userHandle != IntPtr.Zero)
                {
                    CloseHandle(userHandle);
                }
            }

        }
        catch (Exception)
        {
        }
    }

【问题讨论】:

  • 你可能想看看它抛出的异常。
  • 确保运行您的应用程序的用户具有写入共享驱动器的适当权限。
  • 它没有任何异常。我也测试过。
  • 注释掉 catch (Exception) {} - boo btw.;重试并告诉我们它抛出了什么异常。
  • 你有两个空的 catch 块。把它们全部扔掉,永远不要再使用它们。

标签: c# asp.net copy


【解决方案1】:

您可以像这样设置模拟用户类:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;

public class ImpersonatedUser : IDisposable
{
    IntPtr userHandle;
    WindowsImpersonationContext impersonationContext;

    public ImpersonatedUser(string user, string domain, string password)
    {
        userHandle = IntPtr.Zero;
        bool loggedOn = LogonUser(
            user,
            domain,
            password,
            LogonType.Interactive,
            LogonProvider.Default,
            out userHandle);

        if (!loggedOn)
            throw new Win32Exception(Marshal.GetLastWin32Error());

        // Begin impersonating the user
        impersonationContext = WindowsIdentity.Impersonate(userHandle);
    }

    public void Dispose()
    {
        if (userHandle != IntPtr.Zero)
        {
            CloseHandle(userHandle);
            userHandle = IntPtr.Zero;
            impersonationContext.Undo();
        }
    } 

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        LogonType dwLogonType,
        LogonProvider dwLogonProvider,
        out IntPtr phToken
        );

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool CloseHandle(IntPtr hHandle);

    enum LogonType : int
    {
        Interactive = 2,
        Network = 3,
        Batch = 4,
        Service = 5,
        NetworkCleartext = 8,
        NewCredentials = 9,
    }

    enum LogonProvider : int
    {
        Default = 0,
    }
}

当你需要复制文件时,你可以这样做:

    using (new ImpersonatedUser(<UserName>, <UserDomainName>, <UserPassword>))
    {
        DoYourFileCopyLogic();
    }

【讨论】:

  • 你通常不想这样做。
  • @N. Jensen:谢谢你的尝试。其他只是在 cmets 上浪费时间。不错的尝试兄弟!
  • @Avi 你是绝对正确的。我们在浪费时间。
  • 这可以完美地将文件复制到网络驱动器文件夹。非常感谢!:)
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 2013-04-19
  • 2019-11-14
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
相关资源
最近更新 更多