Showing posts with label .net core. Show all posts
Showing posts with label .net core. Show all posts

Sunday, May 14, 2023

Password encryption option so even DBA can’t see the password in .NET core

 In .NET Core, you can use cryptographic functions to encrypt passwords and securely store them in a database. One common approach is to use a one-way hashing algorithm with a salt. Here's a simplified example of how you can accomplish this:

1. Add the necessary NuGet package: Install the System.Security.Cryptography package to gain access to cryptographic functions.

2. Generate a salt: A salt is a random value that adds uniqueness to each hashed password, making it harder to crack. You can generate a salt using a cryptographic random number generator. Here's an example:

byte[] salt = new byte[16];

using (var rng = RandomNumberGenerator.Create())

{

    rng.GetBytes(salt);

}

3. Hash the password: Use a secure hashing algorithm, such as bcrypt, PBKDF2, or Argon2, to hash the password along with the salt. The salt should be stored alongside the hashed password in the database. Here's an example using the bcrypt algorithm:


string password = "myPassword";

string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password, salt: salt);

4. Verify a password: When a user attempts to log in, you can verify their password by comparing the stored hashed password with the newly hashed password using the same salt. Here's an example:

string userEnteredPassword = "myPassword";

bool passwordMatches = BCrypt.Net.BCrypt.Verify(userEnteredPassword, hashedPassword);

By following these steps, even a DBA with access to the database would not be able to see the original password, as it is never stored in plain text. Only the hashed password and the salt are stored, and the verification process compares the hashed values.


Tuesday, December 27, 2022

How to make sure unmanaged memory release .net core ?

 In .NET Core, unmanaged memory is typically allocated and released through the use of pointers and the Marshal.AllocHGlobal and Marshal.FreeHGlobal methods. These methods are part of the System.Runtime.InteropServices namespace and are used to allocate and release unmanaged memory in the global heap.


To ensure that unmanaged memory is properly released, you should make sure to call the FreeHGlobal method when you are finished using the memory. You can do this in a finally block to ensure that the memory is released even if an exception is thrown. Here is an example of how you might use these methods to allocate and release unmanaged memory in .NET Core:



using System; using System.Runtime.InteropServices; namespace UnmanagedMemoryExample { class Program { static void Main(string[] args) { // Allocate unmanaged memory IntPtr ptr = Marshal.AllocHGlobal(1024); try { // Use the unmanaged memory here... } finally { // Release the unmanaged memory Marshal.FreeHGlobal(ptr); } } } }

Note that you should only use the global heap for small blocks of memory that are needed for a short period of time. For larger blocks of memory or for memory that is needed for a longer period of time, you should consider using the Marshal.AllocCoTaskMem method or creating a managed wrapper class that can be used to manage the memory.

It is also a good idea to use the using statement whenever possible to automatically release resources when they are no longer needed. This can help simplify your code and reduce the chances of resource leaks.

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...