访问被拒绝的文件在asp.net核心 [英] Access denied file in asp.net core

查看:153
本文介绍了访问被拒绝的文件在asp.net核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当我将项目发布到iis并在浏览器上上传图片时,此操作失败并在logger sysytem


When i publish my project to iis and i do upload a picture on browser so this operation is fails and show this error in logger sysytem

发生未处理的异常:拒绝访问路径'C:\ Inetpub \ vhosts \ qarbal.com \ back.qarbal.com \ wwwroot \ images \ UserProfile \ pic_50.jpg'.路径'C:\ Inetpub \ vhosts \ qarbal.com \ back.qarbal.com \ wwwroot \ images \ UserProfile \ pic_50.jpg'被拒绝. 在System.IO.FileStream.OpenHandle(FileMode模式,FileShare共享,FileOptions选项) 在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项)

An unhandled exception has occurred: Access to the path 'C:\Inetpub\vhosts\qarbal.com\back.qarbal.com\wwwroot\images\UserProfile\pic_50.jpg' is denied.System.UnauthorizedAccessException: Access to the path 'C:\Inetpub\vhosts\qarbal.com\back.qarbal.com\wwwroot\images\UserProfile\pic_50.jpg' is denied. at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)

ActionController

ActionController

[HttpPost("[action]")]
public async Task<IActionResult> EditPhoto()
{
    var uploadsRootFolder = Path.Combine(_env.WebRootPath, "images\\UserProfile");
    var files = Request.Form.Files;     
    foreach (var file in files)
    {       
        if (file == null || file.Length == 0)
        {
            continue;
        }
        var filePath = Path.Combine(uploadsRootFolder, queryModel.Name  + files[0].FileName);
        using (var fileStream = new FileStream(filePath, FileMode.Create))
        {
            await file.CopyToAsync(fileStream).ConfigureAwait(false);
        }
    }
    return Json("ok");  
}

startup.cs

public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();
}

如何解决此异常?

推荐答案

右键单击wwwroot文件夹->属性->安全选项卡->单击Edit按钮->输入IIS AppPool\DefaultAppPool用户->单击在Check names-> OK->然后将其命名为Write permission.

Right click on the wwwroot folder -> Properties -> Security tab -> Click at Edit button -> Enter IIS AppPool\DefaultAppPool user -> Click at Check names -> OK -> Then give it Write permission.

这篇关于访问被拒绝的文件在asp.net核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆