asp.net – File.Exists从网络共享返回false
发布时间:2020-05-24 04:11:08 所属栏目:asp.Net 来源:互联网
导读:我一直在研究一个将上传的文件保存到网络共享的ASP.NET项目.我想我可以使用虚拟目录并且没问题,但我一直在努力获得Directory.CreateDirectory的权限. 我能够上传文件,所以我决定更改我的代码,将所有内容放在一个目录中,但这需要我使用File.Exists来避免重写.
|
我一直在研究一个将上传的文件保存到网络共享的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(); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-ajax – Sys.Application.add_load()vs. $(documen
- vbscript – 在赋值中使用Set时“需要对象”
- asp.net – 我怎么知道我的应用程序中是否需要“WCF HTTP激
- asp.net – HttpContext.Current如何工作?
- asp.net-mvc – ASP.net MVC RTM测试命名约定
- asp.net-mvc-3 – dataannotations在主键上设置标识种子值,
- 在对ASP.NET MVC Action的AJAX请求期间有网络请求超时时会发
- asp.net – 在日期字段中插入空值?
- asp.net-mvc – 第一个Web API会话请求非常慢
- asp.net-mvc – Ninject – 具有参数/ Entity Framework连接
推荐文章
站长推荐
- asp.net-mvc-4 – 我似乎没有安装SignalR与MVC4
- asp.net – 如何使用ValueInjector映射列表
- ASP.NET计算访问者,而不是机器人
- asp.net – 如何防止Entity Framework将FileStre
- asp.net核心 – 无法使用托管代理在Visual Studi
- asp.net – 您可以/应该在用户会话对象中存储多少
- asp.net-mvc – 如何在ASP.NET MVC部分视图中使用
- CookieAuthenticationOptions,ExpireTimeSpan不起
- asp.net-mvc – ASP.NET MVC – 从视图部分更新模
- 需要ASP.Net/MVC Rich Text Editor
热点阅读
