Posts

Creating a Responsive Image Component in Next.js with TypeScript

Image
In this tutorial, we'll create a  ResponsiveImg  component that uses the Next.js Image component for optimized  image  rendering.   Introduction The ResponsiveImg Component The ResponsiveImg component accepts properties for both mobile and desktop versions of the image. It uses the useEffect and useState hooks to determine the screen size and adjust the image properties accordingly. Here's the complete code for the ResponsiveImg component: /**  * ResponsiveImg component that renders an image which adapts to screen size.  * It uses the Next.js Image component for optimized image rendering.  *  * @component  * @param {ResponsiveImgProps} props - The properties for the ResponsiveImg component.  * @param {Object} props.mobile - The properties for the mobile version of the image.  * @param {string | StaticImport} props.mobile.src - The source of the mobile image.  * @param {number} props.mobile.width - The width of the mobile image.  * @param {number} props.mobile.hei

try...finally in JavaScript and TypeScript

Image
 In JavaScript, you can use try...finally to ensure that certain cleanup code runs regardless of whether an error occurs. This is useful for releasing resources like memory, files, or network connections. Example: Using try...finally for Cleanup Here's an example of how you might use try...finally to ensure that a file is closed after it has been read, regardless of whether an error occurs during the reading process: const fs = require ( 'fs' ); function readFile ( filePath ) {   let fileDescriptor ;   try {     fileDescriptor = fs . openSync ( filePath , 'r' );     const buffer = Buffer . alloc ( 1024 );     fs . readSync ( fileDescriptor , buffer , 0 , buffer . length , 0 );     console . log ( buffer . toString ());   }   catch ( error ) {     onsole . error ( 'Error reading file:' , error );   }   finally {     if ( fileDescriptor !== undefined ) {       fs . closeSync ( fileDescriptor ); console . log ( 'File closed' );     }  

Hashing password in Next JS against rainbow table attacks with a salt for login page

If you want to send a username and password to the server side in nextJs and check if it is correct to continue the identification process. In this article I will explain how and explain why it is important to add salt to a salad 😁 First I will explain why hashing passwords with a salt is an effective defense against rainbow table attacks . Here's why: Rainbow Tables Explained : A rainbow table is a precomputed table for reversing cryptographic hash functions, primarily used for cracking password hashes. Suppose an attacker has a database of hashed passwords obtained from a compromised system. If these passwords were hashed without a salt, the attacker could use a rainbow table to look up the hash values and find the corresponding plaintext passwords. Rainbow tables are feasible because they can be precomputed for all possible plaintext passwords up to a certain length and complexity. The Role of Salt : A salt is a random value that is added to the password before it is hashed. Th

How to create a security ASP.NET Web API controller

 Hi, Here is a code that has base response class and use it in an ASP.NET Web API controller to send a response with a cookie and security headers. This code creates a BaseResponse<T> class with a Status property and a Data property of type T. In the Post method of MyController, a new HttpResponseMessage is created with a status of OK and the content set to a serialized BaseResponse<string>. A cookie is added to the response headers, and then several security headers are added. The X-Content-Type-Options header is set to nosniff to prevent the browser from trying to interpret content with a MIME type that doesn't match the declared type. The X-Frame-Options header is set to SAMEORIGIN to prevent the page from being displayed in a frame or iframe. The Content-Security-Policy header is set to only allow content from the same origin In this code, I've assumed that you have a service IDatabaseService with a method CheckIfExists that checks if an ID exists in the databas

React lazy component (import component)

Image
Let's face it - there are some problems with React & Webpack. When I create a Webpack bundle - All the files (js and css) are merged. (remember files Name Must Start with Capital letter- js , css,..) It means that if I have some styles with the same name, the style will be applied on all the elements. Ex: In A component - there is an element like this: <Button className="mybutton" label="close" /> with style like this: .mybutton {color:red} In B component - there is element with the same class name <Button className="mybutton" label="open" /> So because I used the same style css class name - the Webpack bundle merged the css files of all the components, its means that we got red color to all components that use the same name. In the example component B got red color for the button. I call it a bug (not a feature 😀) In my project I created an app.js. In this file I import all the components. There, I noticed that as I mentione

Limit access to your IIS site to specific security Active Directory groups

You can limit access to your IIS site to specific security Active Directory (AD) groups. Here are the steps to do this: Step 1 : Create or Identify the Security Group in Active Directory 1. Open Active Directory Users and Computers (ADUC)  on your domain controller. 2. Navigate to the Organizational Unit (OU)  where you want to create the group. 3. Right-click the OU, select New  > Group . 4. Name your group and ensure the Group scope  is set to Security . 5. Add users to this group who should have access to the IIS site. Step 2: Configure IIS to Use Windows Authentication 1. Open IIS Manager  on your web server. 2. Select your site from the Connections  pane. 3. Double-click Authentication in the Features View . 4. Enable   Windows Authentication  and Disable  other authentication methods like Anonymous Authentication . Step 3 : Set NTFS Permissions for the Site Folder 1. Navigate to the folder containing your IIS site's content. 2. Right-click the folder, select Properties

How to get the following ID in a SharePoint list

 hi... More than a decade ago .. I wrote a kwizcom blog article - how to get the following ID in a SharePoint list https://kwizcom.blogspot.com/2009/08/how-do-i-tell-what-next-list-item-id-is.html This was valid for SharePoint 2007 and 2010 for sure. I do not know when Microsoft changed the table in the database .. But now in SharePoint 2019 they have created another table AllListsAux and the code should look like this /// Get the next available item id from a list /// < name="site">site /// < name="listId">listId /// public static int SharePointListNextItemId(SPSite site, Guid listId) {     int id = -1;     SPSecurity.RunWithElevatedPrivileges(delegate ()     {         if (site.WebApplication.ContentDatabases.Count > 0)         {             string DBConnString = site.WebApplication.ContentDatabases[0].DatabaseConnectionString;             using (SqlConnection con = new SqlConnection(DBConnString))             {                 try