簡體   English   中英

無法在容器啟動時更改某些 js/html 文件內容。 獲取訪問被拒絕異常

[英]Unable to change some js/html file content on container start. getting access denied exception

我有一個案例,我需要在編譯的 vuejs js/html 文件中注入一些環境變量值,該文件駐留在我的 .net core 3.1 應用程序中。 我已經將內容更改邏輯放在了啟動.cs 的配置方法中。 我收到拒絕訪問異常我的代碼是

private void ReplaceEnvironmentVariableWithVal()
        {
            try
            {
                string apiurlToken = "VUE_APP_BEAPI_URL";
                string relevantPath = "VUE_APP_RELEVANTPATH";
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "clientapp", "dist", "js");
                Console.WriteLine($"{apiurlToken} env value is {GetEnvVariable(apiurlToken)}");
                Console.WriteLine($"{relevantPath} env value is {GetEnvVariable(relevantPath)}");
                Console.WriteLine("Application Root Path of js files " + path);
                DirectoryInfo dir = new DirectoryInfo(path);
                Dictionary<string, string> dict = new Dictionary<string, string>();
                if (dir != null)
                {
                    foreach (var file in dir.GetFiles())
                    {
                        if (file.Extension == "js" || file.Extension == ".js")
                        {
                            var fileContent = File.ReadAllText(file.FullName);
                            if (!String.IsNullOrWhiteSpace(fileContent))
                            {

                                if (fileContent.Contains(apiurlToken))
                                {
                                    string variableValue = GetEnvVariable(apiurlToken);
                                    if (!String.IsNullOrWhiteSpace(variableValue))
                                    {
                                        fileContent = fileContent.Replace(apiurlToken, variableValue);
                                        dict[file.FullName] = fileContent;
                                    }

                                }

                                if (fileContent.Contains(relevantPath))
                                {
                                    string variableValue = GetEnvVariable(relevantPath);
                                    //if (!String.IsNullOrWhiteSpace(variableValue))
                                    {
                                        fileContent = fileContent.Replace(relevantPath, variableValue);
                                        dict[file.FullName] = fileContent;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Application Root Path of js files not exists");
                }
              
                foreach (KeyValuePair<string, string> entry in dict)
                {
                    Console.WriteLine("Updating " + entry.Key);
                    using (var fs = new FileStream(entry.Key, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        using var sr = new StreamWriter(fs);
                        sr.Write(entry.Value);
                    }
                    Console.WriteLine("Update Completed " + entry.Key);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in ReplaceEnvironmentVariableWithVal:" + ex.Message);
            }
        }

我已經實現了我賦予用戶權限的文件的寫入。 我已經記錄了運行應用程序的用戶,它是默認的。 然后在我的 docker 文件 COPY --chown=default:default 中添加了以下行。 .

基本上賦予用戶權利

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM