【发布时间】:2014-10-26 19:33:16
【问题描述】:
我有一个带有实体框架的 mvc4 应用程序。
我想删除一个文件,但每次都说:
mscorlib.dll 中出现“System.UnauthorizedAccessException”类型的异常,但未在用户代码中处理
附加信息:对路径“G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\”的访问被拒绝。
通过这一行:System.IO.File.Delete(path);
这是方法:
public ActionResult DeleteFiles(int id)
{
//var fileName = Path.GetFileName(id.FileName);
var DirSeparator = Path.DirectorySeparatorChar;
var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
var file = db.lolabikerPhotos.Find(id);
System.IO.File.Delete(path);
db.SaveChanges();
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
我只是在 Visual Studio 中运行该应用程序,如下所示:http://localhost:41787/Account/Edit?UserId=hallo
我已经做了以下事情:
完全访问地图,我将网络服务添加到完全控制的地图中。但似乎没有任何效果。我使用的是 Windows 7。我以管理员身份运行 Visual Studio 2013
我也看到了:
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
这里可以看到访问权限:
我尝试这样的事情:
<system.web>
<identity impersonate="true" userName="Administrator" password="windowsPassword"/>
<httpRuntime requestValidationMode="2.0" maxRequestLength="1048576" executionTimeout="3600" />
<compilation debug="true" targetFramework="4.5" />
<pages validateRequest="false" />
<!--<httpRuntime targetFramework="4.5" />-->
</system.web>
我已经添加了这个:
<identity impersonate="true" userName="Administrator" password="windowsPassword"/>
好的,我可以运行应用程序,但仍然报错:
Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Source Error:
Line 489: var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
Line 490: var file = db.lolabikerPhotos.Find(id);
Line 491: System.IO.File.Delete(path);
Line 492: db.SaveChanges();
Line 493:
完全权限:
高级选项卡:
更改权限选项卡:
我像这样编辑了我的操作方法:
public ActionResult DeleteFiles(int id)
{
var fileName = Path.GetFileName(@"\\Koala.jpg");
var DirSeparator = Path.DirectorySeparatorChar;
var path = Server.MapPath(@"\\Images" + DirSeparator + fileName.Replace('+', '_'));
var file = db.lolabikerPhotos.Find(id);
LolaBikePhoto lola = db.lolabikerPhotos.Find(id);
db.lolabikerPhotos.Remove(lola);
System.IO.File.Delete(path);
db.SaveChanges();
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
现在它正在工作!
【问题讨论】:
-
大多数应用程序在常规用户访问权限下运行,无论您的帐户是否为管理员。只是想确定一下,您肯定是以管理员身份运行应用程序本身吗?另外,请确保您要删除的文件没有设置其只读属性。
-
我可以上传文件,但不能删除文件,我还检查了路径,这是正确的我以管理员身份运行 Visual Studio
-
进入要授予访问权限的文件夹属性中的权限选项卡。取得文件夹的所有权并将更改传播到所有子文件夹和文件。然后向每个人授予完全权限并传播这些更改。确保应用所有更改。一旦您确认它有效,您就可以重新保护您的权限。如果您在任何权限更改方面需要帮助,这里有很多关于它的文章(只有 Google)
-
我已经做到了,看我的帖子
-
我只是略过了你的问题,但无论你在做什么都可能过于复杂。在 IIS 中,在 Authentication 下,将 Anonymous Authentication 设置为 Application pool identity。然后将目录权限授予运行应用程序池的用户。理想情况下,您希望为您的应用程序池使用托管服务帐户,以便轻松设置网络共享权限,但这不是必需的。
标签: c# windows entity-framework asp.net-mvc-4 visual-studio-2013