微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – File.Exists从网络共享返回false

我一直在研究一个上传文件保存到网络共享的ASP.NET项目.我想我可以使用虚拟目录并且没问题,但我一直在努力获得Directory.CreateDirectory的权限.

我能够上传文件,所以我决定更改我的代码,将所有内容放在一个目录中,但这需要我使用File.Exists来避免重写.

现在我已经更新了所有代码,我发现无论我做什么,当我测试网络共享时,File.Exists总是返回false(文件肯定存在).

有任何想法吗?我正在通过网络共享走到尽头.

解决方法

我刚刚参与了一个非常类似的项目,我将文件保存到网络共享.这两台计算机位于同一子网上,但不受域控制器控制,因此每台计算机都拥有自己的用户.

我在两台计算机上创建了一个用户名和密码相同的用户.然后我创建了一个网络共享并设置文件夹/共享权限以允许用户进行读写.

然后我创建了以下类来管理模拟:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.Text;

namespace MyProject.Business.Web
{
    public class SecurityManager
    {
        #region DLL Imports
        [DllImport("advapi32.dll",SetLastError = true,CharSet = CharSet.Unicode)]
        public static extern bool logonUser(String lpszUsername,String lpszDomain,String lpszPassword,int dwlogonType,int dwlogonProvider,ref IntPtr phToken);

        [DllImport("kernel32.dll",CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);

        [DllImport("advapi32.dll",CharSet = CharSet.Auto,SetLastError = true)]
        public extern static bool Duplicatetoken(IntPtr ExistingTokenHandle,int Security_IMPERSONATION_LEVEL,ref IntPtr DuplicatetokenHandle);
        #endregion

        public string Domain { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        private WindowsImpersonationContext m_CurrentImpersonationContext;

        [PermissionSetAttribute(SecurityAction.Demand,Name = "FullTrust")]
        public void StartImpersonation()
        {
            const int logoN32_PROVIDER_DEFAULT = 0;
            const int logoN32_logoN_INteraCTIVE = 2;

            IntPtr tokenHandle = IntPtr.Zero;
            IntPtr dupetokenHandle = IntPtr.Zero;

            // obtain a handle to an access token
            bool waslogonSuccessful = logonUser(UserName,Domain,Password,logoN32_logoN_INteraCTIVE,logoN32_PROVIDER_DEFAULT,ref tokenHandle);

            if (!waslogonSuccessful)
                throw new Exception(String.Format("logon Failed with error number {0}",Marshal.GetLastWin32Error()));

            // use the token handle to impersonate the user
            WindowsIdentity newId = new WindowsIdentity(tokenHandle);
            m_CurrentImpersonationContext = newId.Impersonate();

            // free the tokens
            if (tokenHandle != IntPtr.Zero)
                CloseHandle(tokenHandle);
        }
        public void EndImpersonation()
        {
            m_CurrentImpersonationContext.Undo();
        }
    }
}

然后在ASP.NET页面中,我执行了以下操作:

SecurityManager sm = new SecurityManager();
sm.UserName = ConfigurationManager.AppSettings["UserFileShareUsername"];
sm.Password = ConfigurationManager.AppSettings["UserFileSharePassword"];
sm.StartImpersonation();

if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);

File.Move(sourcePath,destinationPath);

sm.EndImpersonation();

原文地址:https://www.jb51.cc/aspnet/246855.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐