Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access denied file in asp.net core


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

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

[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();
}

How to solve this Exception ?

like image 839
Mohammad Daliri Avatar asked Feb 22 '18 17:02

Mohammad Daliri


People also ask

How do I grant ASP NET Access to a file?

To grant ASP.NET access to a file, right-click the file in 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.

What is the base process identity (ASPnet)?

ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) 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.

How do I add permissions to a directory in ASPnet?

To add permissions to a directory, perform the following steps: Click the "Add" button and enter the machine name followed by the ASPNET account name. For example, on a machine named "webdev", you would enter webdevASPNET and hit "OK".


1 Answers

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.

like image 73
VahidN Avatar answered Sep 24 '22 05:09

VahidN