简体   繁体   中英

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 在此处输入图像描述

This error may cause by your asp.net account {MACHINE}\ASPNET does not have write access to the path '\kalbox\Video\1231'.

You can try below steps to solve this question:

Right click on folder Properties > Security Tab > Edit > Add > locations > choose your local machine > click OK > Type ASPNET below "Enter the object name to select" > Click Check Names Check the boxes for the desired access (Full Control). If it will not work for you do the same with Network Service.

Now this should show your local {MACHINENAME}\ASPNET account, then you set the write permission to this account.

Otherwise if the application is impersonating via, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM