Search code examples
javascriptc#model-view-controlleriis-8access-denied

Insert Folder C# with MVC IIS 8 Error An unhandled access exception has occurred


I Debugging with locally on Windows 10 Pro, VS 2019. This will run just fine on my local machine, but not on the web server my Server is Windows Server 2012 R2 with IIS 8. Does anyone have any ideas as to what the problem might be?

Here My Code :

View

$.ajax({
                        url: 'AddFolder',
                        type: 'post',
                        data: JSON.stringify(dto),
                        dataType: 'json',
                        contentType: 'application/json;charset=utf-8',
                        success: function (data) {
                            alert("Insert Folder Success");
                            $('#foldername').val("");
                        },
                        error: function (ex) {
                            alert(JSON.stringify(ex));
                        }
                    });

Controller

 public ActionResult AddFolder(videopath model)
        {
            string conSQL = mySetting.ConnectionString;
            SqlDataAdapter dataAdapt = new SqlDataAdapter();
            SqlConnection conn = new SqlConnection(conSQL);
            List<string> ModelData = new List<string>();
            string result;

            try
            {

                string path1 =@"\\kalbox\Video\";
                string path2 = Path.Combine(path1, model.FolderName);
                if (!Directory.Exists(path2))
                {
                    Directory.CreateDirectory(path2);
                    using (SqlCommand command = new SqlCommand("InsertFolderPath", conn))
                    {
                        conn.Open();
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.Add("@FolderName", System.Data.SqlDbType.VarChar);
                        command.Parameters["@FolderName"].Value = model.FolderName;
                        command.Parameters.Add("@FolderPath", System.Data.SqlDbType.VarChar);
                        command.Parameters["@FolderPath"].Value = path2;


                        result = (string)command.ExecuteScalar();
                        ModelData.Add(result);
                        conn.Close();

                    }
                }
                else
                {
                    Console.WriteLine("Folder Exists");
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return Json(ModelData, JsonRequestBehavior.AllowGet);
        }

Here my Error :

  • ASP.NET web page output: Access to the path '\\kalbox\Video\1231' is denied

  • Application Event Log:

    Event code: 4011 
    Event message: An unhandled access exception has occurred. 
    Event time: 4/15/2021 4:41:57 PM 
    Event time (UTC): 4/15/2021 9:41:57 AM 
    Event ID: 0c49e37dc23b486b8537561bbc024a5b 
    Event sequence: 7 
    Event occurrence: 1 
    Event detail code: 0 
     
    Application information: 
        Application domain: /LM/W3SVC/1/ROOT/Video_OPL-66-132629533163447222 
        Trust level: Full 
        Application Virtual Path: /Video_OPL 
        Application Path: D:\Video_Training\ 
        Machine name: SERVERNAME
     
    Process information: 
        Process ID: 12844 
        Process name: w3wp.exe 
        Account name: NT AUTHORITY\NETWORK SERVICE 
     
    Request information: 
        Request URL: http:********
        Request path: /Video_OPL/Home/AddFolder 
        User host address: ****** 
        User: domain\Agus.Suhardi 
        Is authenticated: True 
        Authentication Type: Negotiate 
        Thread account name: NT AUTHORITY\NETWORK SERVICE 

  • F12 Console Browser Google Chrome : Failed to load resource: the server responded with a status of 500 (Internal Server Error)

i already find my Windows authentication, and message box show my name (domainAgus.Suhardi, no slash like domain\Agus.Suhardi )

var winlogon = '@HttpContext.Current.User.Identity.Name';
alert (winlogon);

Here my Permission Kalbox Folder

FYI folder kalbox only add user AD (Active Directory) and My IIS Username is

NT AUTHORITY\NETWORK SERVICE enter image description here


Solution

  • I already Solve for my case you can try add user Everyone and give Full control

    btw tq Samwu for your answer and you are right my account IIS does not have write access