adsense

Saturday, April 20, 2024

ASP.NET Web Applications with Content Security Policy


 Content Security Policy (CSP) can be used to protect any ASP.NET web application by identifying  

  • Cross-site Scripting (XSS) attacks
  • SQL or data injection attacks.
We can use the web.config to configure CSP and this article explains various directives used for CSP and use of it.

Cheers,

Samitha

Monday, April 8, 2024

.Net core 6 db migration error

 I got following error when trying to run the migrations on a .NET Core 6 Web API.

Unable to create an object of type 'MyDBContext'

After tarting out different approaches following  changes made migrations work.


1. MyDbContext 

  

 public class MyDbContext : DbContext, IMyDbContext

    {

        public MyDbContext()

        {

            

        }

        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)

        {

            

       

        protected override void OnConfiguring(DbContextOptionsBuilder

 optionsBuilder)

        {

            optionsBuilder.UseSqlServer("Data Source=localhost;Initial Catalog = Library;Integrated Security=True;Trusted_Connection=True;TrustServerCertificate=True");

        }

//...more code


}

2. Program.cs

builder.Services.ConfigureLoggerService(); 

//more options

options.UseSqlServer(builder.Configuration.GetConnectionString

("DefaultConnection")));


Cheers,

Samitha 

Friday, March 29, 2024

Append JavaScript to html body

      

Following JavaScript code will append an external JavaScript file to the document body. This can be used as guide to load a JavaScript file on the fly.

<script type="text/javascript">

var newScript = document.createElement('script');

newScript.type = 'text/javascript';

 newScript.async = true;

 newScript.src = 'https://somedomain.com/JsFileName.js'; 

document.getElementsByTagName("body")[0].appendChild(newScript);

 </script>

cheers,

Samitha

Sunday, March 3, 2024

Auto Generate .gitignore file

 This is a configuration file that tells Git what to ignore when committing changes to Git based repositories (the ones in GitHub).

 The .gitignore file controls what  files to ignore  when committing changes to Git repositories. The good new is is there is a tool called gitignore.io. This tool will generate such files for us.

I like this as it allows to type programming languages/platforms and generates the file for us

 Cheers,

Samitha

Friday, January 26, 2024

C# download file from a given URL

 If you want to download a file  in C # form a given url following code snippet can be used.

 public static async Task Download(string remoteUri,   string directory)

        {

            try

            {

               

                string filePath = Path.Combine(directory, Path.GetFileName(remoteUri));

                var httpClient = new HttpClient();

                if (!Directory.Exists(directory))

                {

                    Directory.CreateDirectory(directory);

                }

                using var stream = await httpClient.GetStreamAsync(remoteUri);

                using var fileStream = new FileStream(filePath, FileMode.Create);

                 await stream.CopyToAsync(fileStream);


            }

            catch (Exception)

            {

                throw;

            }


        }


 When calling the above method we can user as

Download("https://abc.com/Content/Images/imagename.jpg", Directory.GetCurrentDirectory() + "\\images\\").Wait();

Note that this code is compatible with .Net 6 and .Net 7.

cheers,

Samitha

Sunday, January 14, 2024

C # get file MIMETYPE

 If we want to determine the mime type of a given file, we can use the MimeTypeMap NuGet package. I found it very efficient and up to date with additions from many contributors.


Cheers,

Samitha