Thursday, January 23, 2020

Check File Permission for App Service Files in Azure


Below is the permissions output of files in the wwwroot directory that I ran with a Powershell command in Kudu site. So if you ever see the issue again and you ensure stdoutLogFile="\\?\%home%\LogFiles\stdout" but still have issues, you may compare your Powershell permissions output of your wwwroot directory to mine to see if they have same access control:

You should use this when your web application isn't starting and giving you the permission error. 

Navigate to your Kudu site (yourApp.scm.azurewebsites.net) --> Debug Console --> PowerShell --> navigate to wwwroot directory





(Run the PS command below to show all files access control and pipe output to avoid PS truncation of the results)

Dir | Get-Acl | Format-List

You can compare your output to mine:
F

PS D:\home\site\wwwroot> Dir | Get-Acl | Format-List
Dir | Get-Acl | Format-List​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\wwwroot​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;OICIID;0x1301ff;;;WD)(A;OICIID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\appsettings​
         .Development.json​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\appsettings​
         .json​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\hostingstar​
         t.html​
Owner  : BUILTIN\Administrators​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:BAG:S-1-5-21-2996674325-4160296812-1073090115-513D:AI(A;ID;0x1301ff;​
         ;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\web.config​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\WebApplicat​
         ion4.deps.json​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\WebApplicat​
         ion4.dll​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\WebApplicat​
         ion4.pdb​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\WebApplicat​
         ion4.runtimeconfig.json​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\WebApplicat​
         ion4.Views.dll​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)​
Path   : Microsoft.PowerShell.Core\FileSystem::D:\home\site\wwwroot\WebApplicat​
         ion4.Views.pdb​
Owner  : O:S-1-5-21-2996674325-4160296812-1073090115-501​
Group  : G:S-1-5-21-2996674325-4160296812-1073090115-513​
Access : Everyone Allow  DeleteSubdirectoriesAndFiles, Modify, Synchronize​
         BUILTIN\Administrators Allow  FullControl​
Audit  : ​
Sddl   : O:S-1-5-21-2996674325-4160296812-1073090115-501G:S-1-5-21-2996674325-4​
         160296812-1073090115-513D:AI(A;ID;0x1301ff;;;WD)(A;ID;FA;;;BA)


Sunday, December 15, 2019

Delete files older than x days on Azure Web App



Get files older than 30 days.
forfiles -s -m *.* -d -30 -c "cmd /c echo @file" (All files)


Once results look ok, change echo to delete. 
forfiles -s -m *.* -d -30 -c "cmd /c del @file"

Change *.* to * if you want to include directories. 

to have a condition


forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==TRUE echo @file is a directory"

Remove all directories from the current directory older than 3 days.

forfiles -s -m * -d -3 -c "cmd /c if @isdir==TRUE RMDIR /S /Q @file"


Tuesday, October 15, 2019

HangFire Ioc Contaner - Job Schedular

Nuget :
Hangfire.AspNetCore
Hangfire.SqlServer (Redis can also be used)

services.AddHangfire(x => {
                var options = new SqlServerStorageOptions
                {
                    PrepareSchemaIfNecessary = true,
                    QueuePollInterval = TimeSpan.FromSeconds(5)
                };
                x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"), options);
                });


 //services.AddHangfire(configuration => {
            //    configuration.UseStorage(
            //        new MySqlStorage(
            //            "server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True",
            //            new MySqlStorageOptions
            //            {
            //                TablesPrefix = "Hangfire"
            //            }
            //        )
            //    );
            //};


//https://docs.hangfire.io/en/latest/configuration/using-sql-server.html
            //https://docs.hangfire.io/en/latest/configuration/
            //https://docs.hangfire.io/en/latest/getting-started/aspnet-core-applications.html
            GlobalConfiguration.Configuration.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"));
            GlobalConfiguration.Configuration.UseActivator(new ServiceJobActivator(serviceProvider.GetService<IServiceScopeFactory>(), serviceProvider));
            //GlobalConfiguration.Configuration.UseActivator(new HangfireActivator(serviceProvider));
            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            { 
                
            });
            app.UseHangfireServer(new BackgroundJobServerOptions { 
                 WorkerCount= 1
            });

Job Activator
public class ServiceJobActivator : JobActivator
    {
        readonly IServiceScopeFactory _serviceScopeFactory;
        private readonly IServiceProvider _serviceProvider;

        public ServiceJobActivator(IServiceScopeFactory serviceScopeFactory, IServiceProvider serviceProvider)
        {
            if (serviceScopeFactory == null) throw new ArgumentNullException(nameof(serviceScopeFactory));
            _serviceScopeFactory = serviceScopeFactory;
            _serviceProvider = serviceProvider;
        }

        public override JobActivatorScope BeginScope(JobActivatorContext context)
        {
            return new ServiceJobActivatorScope(_serviceScopeFactory.CreateScope());
        }

        public override object ActivateJob(Type type)
        {
            return _serviceProvider.GetService(type);
        }
    }


public class ServiceJobActivatorScope : JobActivatorScope
    {
        readonly IServiceScope _serviceScope;
        public ServiceJobActivatorScope(IServiceScope serviceScope)
        {
            if (serviceScope == null) throw new ArgumentNullException(nameof(serviceScope));
            _serviceScope = serviceScope;
        }
        public override object Resolve(Type type)
        {
            return _serviceScope.ServiceProvider.GetService(type);
        }
    }


In the service, inject IBackgroundJobClient backgroundJobs and use 
_backgroundJobs.Enqueue<ISampleService>(s => s.CallFunction());




Monday, August 19, 2019

Create Docker container with the image less than 90 days old

Please add -accept_eula in the command to create the container.

Sample Command

New-NavContainer -containerName NAV2017 -accept_eula -accept_outdated -alwaysPull -auth Windows -authenticationEMail test@bizcentralus.onmicrosoft.com -imageName mcr.microsoft.com/dynamicsnav:2017-cu3-na -includeCSide -licenseFile "C:\ABC.flf" -useSSL 

Monday, August 12, 2019

Batch Upload to Azure Storage using Azure CLI

az storage blob upload-batch -d <container_name> -s '<directory_path>' --pattern *.*

  --account-name <account_name>
  --account-key <account_key>