European ASP.NET 4.5 Hosting BLOG

BLOG about ASP.NET 4, ASP.NET 4.5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

European ASP.NET Core 8.0.1 Hosting - HostForLIFE :: Razor Syntax in ASP.NET

clock March 25, 2024 10:23 by author Peter

ASP.NET is a reliable platform for web development, allowing developers to easily create dynamic and resilient web applications. At the heart of ASP.NET is Razor, a powerful templating engine that transforms the way developers create server-side code. Razor syntax provides a concise and easy way to combine server-side code and HTML, increasing productivity and maintainability. Let's go deeper into Razor syntax and see how it can improve your ASP.NET programming experience.

Understanding Razor syntax
Razor syntax seamlessly integrates server-side code into HTML markup, resulting in cleaner and more understandable code than typical ASP.NET Web Forms. Its lightweight and fluid design allows developers to seamlessly switch between HTML and server-side code, resulting in a more efficient development process.

Razor syntax uses a set of special characters (@) to represent server-side code blocks in HTML markup. These code blocks can include C# or VB.NET code, allowing developers to execute logic, get data from databases, modify objects, and conduct a variety of other operations right within the markup.

Benefits of Razor Syntax

  • Readable and Maintainable Code: Razor syntax promotes code readability by minimizing the clutter typically associated with embedding server-side logic within HTML. The clean and concise syntax enhances code maintainability, making it easier for developers to understand and modify code segments as needed.
  • Seamless Integration: Unlike traditional ASP.NET Web Forms, which often necessitate a clear demarcation between server-side and client-side code, Razor syntax seamlessly integrates server-side logic with HTML markup. This cohesive approach simplifies development and fosters a more fluid coding experience.
  • Enhanced Productivity: By streamlining the process of writing server-side code within HTML, Razor syntax boosts developer productivity significantly. Its intuitive nature reduces cognitive overhead, allowing developers to focus on implementing business logic rather than wrestling with cumbersome syntax.
  • Code Reusability: Razor syntax facilitates code reusability by enabling developers to encapsulate common functionality within reusable components known as partial views. These partial views can be invoked from multiple pages, promoting a modular and DRY (Don't Repeat Yourself) coding approach.
  • IntelliSense Support: Integrated development environments (IDEs) such as Visual Studio provide robust IntelliSense support for Razor syntax, offering real-time suggestions and auto-completion for server-side code blocks. This feature enhances code efficiency and reduces the likelihood of syntax errors.

Example of Razor Syntax

<!DOCTYPE html>
<html>
<head>
    <title>ASP.NET Razor Syntax</title>
</head>
<body>
    <div>
        <h1>Welcome, @Model.Name!</h1>
        <p>Your account balance is: [email protected]</p>
        @if (Model.IsPremium)
        {
            <p>Congratulations! You are a premium member.</p>
        }
        else
        {
            <p>Upgrade to premium for exclusive benefits.</p>
        }
    </div>
</body>
</html>

In this example, Razor syntax is used to embed server-side code within an HTML document. The @Model directive accesses properties of the model passed to the view, allowing dynamic content to be rendered based on the provided data.



European ASP.NET Core 8.0.1 Hosting - HostForLIFE :: How to Understanding Deadlocks in C# and .NET Core?

clock March 19, 2024 08:58 by author Peter

Deadlocks are a prevalent problem in concurrent programs, and they can be especially troublesome for programs written in C# and running on.NET Core. A deadlock arises when two or more threads are waiting for each other to release resources that are required to proceed. This can lead to all threads becoming stuck and unable to progress, causing the program to freeze. Many distinct conditions can result in deadlocks, and developers must be aware of them in order to avoid or reduce the danger of deadlocks.

For example, one common circumstance is that two threads each have a resource that the other thread requires. Another instance is when one thread is waiting for a lock to be released while another thread holds that lock and waits for the first thread to release a different lock. These circumstances can be difficult to identify and resolve, but with careful analysis and development techniques, deadlocks can be avoided or reduced.

What is a deadlock?

A deadlock occurs when two or more threads are stalled indefinitely, each waiting for the other to relinquish a resource that it requires to move forward. Deadlocks are characterized by a circular waiting pattern in which no thread can continue, resulting in a stalemate.

using System;
using System.Threading;

class Program
{
static object ResourceA = new object();
static object ResourceB = new object();

static void Thread1()
{
    lock (ResourceA)
    {
        Console.WriteLine("Thread1 acquired ResourceA");
        Thread.Sleep(100); // Simulating some work

        lock (ResourceB)
        {
            Console.WriteLine("Thread1 acquired ResourceB");
        }
    }
}

static void Thread2()
{
    lock (ResourceB)
    {
        Console.WriteLine("Thread2 acquired ResourceB");
        Thread.Sleep(100); // Simulating some work

        lock (ResourceA)
        {
            Console.WriteLine("Thread2 acquired ResourceA");
        }
    }
}

static void Main(string[] args)
{
    Thread t1 = new Thread(Thread1);
    Thread t2 = new Thread(Thread2);

    t1.Start();
    t2.Start();

    t1.Join();
    t2.Join();

    Console.WriteLine("Program completed successfully.");
}
}


Understanding The Deadlock
In this case, Thread1 locks ResourceA first and then attempts to lock ResourceB, whereas Thread2 locks ResourceB first and then attempts to lock ResourceA. If Thread1 obtains ResourceA while Thread2 obtains ResourceB, they will both be waiting for the other resource, resulting in a stalemate.

Preventing Deadlocks

To prevent deadlocks, you can employ various strategies:

  • Lock Ordering: Always acquire locks in a consistent order to prevent circular dependencies.
  • Lock Timeout: Use Monitor.TryEnter or Mutex.WaitOne with a timeout to avoid indefinite blocking.
  • Lock Hierarchy: Establish a lock hierarchy and always acquire locks in the same order within that hierarchy.
  • Avoid Nested Locks: Minimize the use of nested locks to reduce the risk of deadlocks.

Conclusion
Deadlocks can be challenging to debug and resolve, but understanding their causes and employing preventive measures can help mitigate their occurrence. In C# and .NET Core, careful design and coding practices, along with thorough testing, are essential for creating robust and reliable concurrent applications. By following best practices and being mindful of potential deadlock scenarios, developers can ensure the smooth execution of their multithreaded code.



European ASP.NET Core 8.0.1 Hosting - HostForLIFE :: Central Package Management (CPM) in.NET Core

clock March 15, 2024 09:18 by author Peter

Dependency management is a key feature of NuGet. It may be simple to manage dependencies for a single project. As multi-project solutions expand in size and complexity, maintaining dependencies can become difficult.


NuGet's central package management (CPM) features allow you to manage shared dependencies for multiple projects from a single, convenient location.

NuGet package dependencies
NuGet package dependencies have been managed in the following methods.

  • packages.config: An XML file used by earlier project types to keep track of the packages that were referenced.
  • <PackageReference />: NuGet package dependencies are described by an XML element used in MSBuild projects.

Enabling Central Package Management
You must create a Directory.Packages.props file at the root of your repository and set the MSBuild value ManagePackageVersionsCentrally to true in order to begin using central package management.

Then, using <PackageVersion /> elements that identify the package ID and version, you declare each of the corresponding package versions needed for your projects inside.
<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="{{package name}}" Version="{{version number}}" />
  </ItemGroup>
</Project>

For each of the projects, we need to define package references, and each of the projects belongs to the same version.
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="{{package name}}" />
  </ItemGroup>
</Project>


You're controlling your versions centrally and utilizing central package management now!

Let’s check with the demo.
Create one application for whatever you want for the project. Here I have created the console application for demo purposes. In the above image, CPMDemo is the console application, and CPMDemo.Utility is the class library project.

We can create Directory.Packages.props is the root-level file in our console application.

Here is the content of the directory package file.
<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="6.0.27" />
  </ItemGroup>
</Project>


Here is the content of the CPMDemo.csproj changes.
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" />
  </ItemGroup>
</Project>


Here is the content of the CPMDemo.Utility.csproj changes.
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" />
  </ItemGroup>
</Project>


Once you have completed the above changes on your application side, you just need to build the solution, and then after expanding the dependencies and expanding the packages as well, you will see the packages have been added to your application with the mentioned package versions.

Below is a screenshot taken before building the solution, so at that time we hadn’t added any packages to our application.

Below is a screenshot after building the solution and being able to see the added packages in our application.

Central Package Management rules
There are several restrictions governing the location of the Directory.Packages.props file within a repository's directory as well as its context. For the sake of simplicity, each project is evaluated using a single Directory.Packages.props file.

This indicates that the file nearest to the directory of your project will be considered for it if you have several Directory.Packages.props files in your repository. This gives you additional authority over your repository at different levels.

Repository structure
As an illustration, look at the repository structure below:


Here is the explanation for better understanding.

  • ProjectName1 will assess the Directory.Packages.props file in the Repository Name\Solution1\ directory.
  • ProjectName2 will assess the Directory.Packages.props file in the Repository Name\ directory.

In this manner, we can concentrate the packages that we need on the application side. It will also be simple to maintain—we just need to change one location, and it will reflect everywhere.

Happy learning!!



European ASP.NET Core 8.0.1 Hosting - HostForLIFE :: Use ASP.NET to generate a QR code with text around it

clock March 8, 2024 08:07 by author Peter

To generate a QR code with text surrounding it in ASP.Net, use the nuget package listed below. We can use zxing.net or the QRCoder free nuget package. In this case, we'll use the QRCoder package.

Install the QRCoder package on our project. We are good to go now.

Generate QR Code Sample

In Below code snippet we are using QRCodeGenerator Class to generate QRCode, create an instance for QRCodeGenerator(),
QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q)

It will Create QRCode Data for the given text.

ECCLevel

Error Correction Level of the QR code. It represents the ability of the QR code to remain readable even if it's partially damaged or obscured.
  L (Low): About 7% of the codewords can be restored.
  M (Medium): About 15% of the codewords can be restored.
  Q (Quartile): About 25% of the codewords can be restored.
  H (High): About 30% of the codewords can be restored.

It will create QRCode for QR Code data,
QRCode qrCode = new QRCode(qrCodeData);

Converting QR Code into bitmap
GetGraphic(int x);


x: Pixel rate for that QR Code
And we have plenty of override methods for GetGraphic() for different use case scenarios.
Bitmap bitMap = qrCode.GetGraphic(20)

Converting bitmap to base64 string
In bitMap.Save() helps us to save the bitmap in image format.

System.Drawing.Imaging.ImageFormat: we can choose which format we would like to save our image, like (PNG, GIF, JPEG, Icons....etc).

And we are converting our memorystream to byte array then we can convert it into string with base64.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
string base64String = Convert.ToBase64String(byteImage);


Generate Url for QR Code Image
We can define data while returning from API calls, or we can handle that on the UI side.
ImageUrl = "data:image/png;base64," + base64String;

If we just return base64String.
<img  [src]="'data:image/png;base64,'+ base64String" />

Sample for Generate QR Code with text around it.
In Below Sample GetGraphic(7, Color.Black, Color.White, null, 7, 3).

Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true).
  7: pixelsPerModule
  Black: darkColor
  White: lightColor
  null: icon
  7: iconSizePercent
  3: iconBorderWidth

public string GenerateRecurringQRCode(string key){
string result = string.Empty;
QRCodeGenerator qrGenerator = new QRCodeGenerator()
QRCodeData qrCodeData = qrGenerator.CreateQrCode(key, QRCodeGenerator.ECCLevel.Q)
using (QRCode qrCode = new QRCode(qrCodeData))
{
    using (Bitmap bitMap = qrCode.GetGraphic(7, Color.Black, Color.White, null, 7, 3))
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Bitmap qrCodeWithText = AddTextToQRCode(bitMap); // Adding Text to QR Code
            qrCodeWithText.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();
            result = Convert.ToBase64String(byteImage);
        }
    }
}
return result;
}

In this code snippet below we have drawn a DO NOT SCREENSHOT QR CODE around qr code AddTextToQRCode();
static Bitmap AddTextToQRCode(Bitmap bitMap)
 {
     RectangleF rectf1 = new RectangleF(26, 8, 350, 100);
     RectangleF rectf2 = new RectangleF(26, 263, 350, 100);


     Graphics g = Graphics.FromImage(bitMap);

     g.SmoothingMode = SmoothingMode.AntiAlias;
     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
     g.PixelOffsetMode = PixelOffsetMode.HighQuality;

     g.DrawString("DO NOT SCREENSHOT QR CODE", new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, rectf1);
     g.DrawString("DO NOT SCREENSHOT QR CODE", new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, rectf2);


     Rectangle recleft = new Rectangle();
     recleft.Height = 200;
     recleft.Width = 500;
     recleft.X = 0;
     recleft.Y = 0;
     SizeF s;
     String str = "DO NOT SCREENSHOT QR CODE";
     StringFormat strf = new StringFormat();
     strf.Alignment = StringAlignment.Center;
     recleft.X = 0;
     recleft.Y = 0;
     g.TranslateTransform(8, 393);
     g.RotateTransform(-90);
     g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recleft, strf);
     g.ResetTransform();

     Rectangle recright = new Rectangle();
     recright.Height = 200;
     recright.Width = 500;
     recright.X = 0;
     recright.Y = 0;
     SizeF sright;
     String strright = "DO NOT SCREENSHOT QR CODE";
     StringFormat strfright = new StringFormat();
     strf.Alignment = StringAlignment.Center;
     recright.X = 0;
     recright.Y = 0;
     g.TranslateTransform(280, -104);
     g.RotateTransform(90);
     g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recright, strf);
     g.ResetTransform();


     g.Flush();
     return bitMap;
 }

let Break down the above code snippet.

In this, we have created to rectangle to place our text top and bottom
RectangleF rectf1 = new RectangleF(x, y, width, height);
RectangleF rectf1 = new RectangleF(26, 8, 350, 100);

  The X-coordinate of the top-left corner of the rectangle is 26.
  The Y-coordinate of the top-left corner of the rectangle is 8.
  The width of the rectangle is 350.
  The height of the rectangle is 100.

RectangleF rectf1 = new RectangleF(26, 8, 350, 100);
RectangleF rectf2 = new RectangleF(26, 263, 350, 100);


Converting bitmap to graphics and setting quality

  • SmoothingMode: This mode helps to smooth the edges of rendered shapes, resulting in a higher quality appearance, especially when drawing lines or curves.
  • InterpolationMode: The object will interpolate (or estimate) colors and shapes when scaling or rotating images. HighQualityBicubic typically produces smoother results compared to other interpolation modes.
  • PixelOffSetMode: This mode determines how pixels are offset during rendering operations, which can affect the positioning and clarity of rendered objects.

Graphics g = Graphics.FromImage(bitMap);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

Draw String in red color and a bold Arial Black font at the positions defined by rectf1 and rectf2 on the QR code image.
void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle)
  s: "DO NOT SCREENSHOT QR CODE"
  Font: new Font("Arial Black", 10, FontStyle.Bold) We are using Arial Black font and font size 10, fontstyle bold
  Brush: Color of the text we are about to paint (RED)
  layoutRectangle: position and size to a rectangle.

g.DrawString("DO NOT SCREENSHOT QR CODE", new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, rectf1);
g.DrawString("DO NOT SCREENSHOT QR CODE", new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, rectf2);


In this, we have created a rectangle to place our text left and right.

In the code snippet below, we have placed text on the left.
  Rectangle recleft = new Rectangle();: This line creates a new Rectangle object named recleft.
  recleft.Height = 200;: Sets the height of the rectangle to 200.
  recleft.Width = 500;: Sets the width of the rectangle to 500.
  String str = "DO NOT SCREENSHOT QR CODE";: Initializes a string variable str with the text "DO NOT SCREENSHOT QR CODE".
  StringFormat strf = new StringFormat();: Creates a new StringFormat object named strf.
  strf.Alignment = StringAlignment.Center;: Sets the alignment of the text to center.
  recleft.X = 0;: Resets the X-coordinate of the rectangle to 0 (this seems redundant as it was set earlier).
  recleft.Y = 0;: Resets the Y-coordinate of the rectangle to 0 (again, redundant).
  g.TranslateTransform(8, 393);: Translates the origin of the graphics object by (8, 393) pixels or moves the origin of the drawing surface to a specified point.
  g.RotateTransform(-90);: Rotates the graphics object counterclockwise by 90 degrees.
  g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recleft, strf);: Draws the string str using a specified font, brush, and rectangle. The text is drawn with the previously set rotation and translation.
  g.ResetTransform();: Resets the transformation matrix of the graphics object to its identity matrix, undoing the translation and rotation applied earlier.

Rectangle recleft = new Rectangle();
recleft.Height = 200;
recleft.Width = 500;
recleft.X = 0;
recleft.Y = 0;
String str = "DO NOT SCREENSHOT QR CODE";
StringFormat strf = new StringFormat();
strf.Alignment = StringAlignment.Center;
g.TranslateTransform(8, 393);
g.RotateTransform(-90);
g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recleft, strf);
g.ResetTransform();

let do the same for right side.
Rectangle recright = new Rectangle();
recright.Height = 200;
recright.Width = 500;
recright.X = 0;
recright.Y = 0;
String strright = "DO NOT SCREENSHOT QR CODE";
StringFormat strfright = new StringFormat();
strf.Alignment = StringAlignment.Center;
g.TranslateTransform(280, -104);
g.RotateTransform(90);
g.DrawString(str, new Font("Arial Black", 10, FontStyle.Bold), Brushes.Red, recright, strf);
g.ResetTransform();


Flushes any pending graphics operations to ensure that all drawing operations are completed and return bit map to GenerateQRCode() method.
g.Flush();
return bitMap;

Note. If you like to change text you need to alter rectangle and size.
Hope this will be helpful.



ASP.NET Core 8 Hosting - HostForLIFE.eu :: OKTA Authentication for.NET Core API

clock March 6, 2024 08:11 by author Peter

What is Authentication in the.NET Core API?
Authentication is one of the most fundamental aspects of software development. It secures our APIs and refuses API requests if an unauthorized user attempts to access secure endpoints.

Why do we need Authentication in .Net Core API?

  • Data Protection: APIs frequently expose access to data or services that could be abused if they fall into the wrong hands. Authentication ensures that only authorized people or systems can access this information or service.
  • Access Control: Not all users should have equal access rights. Some users may have complete access, while others may have limited permissions. Authentication is the initial step in building such an access control mechanism.
  • Non-repudiation: Using proper authentication, actions may be traced back to the user who conducted them. This ensures accountability for activities taken, which is necessary in many applications.
  • Compliance: Many sectors have standards and obligations in place to protect data and privacy. Authentication, together with other security procedures, helps to meet these regulatory compliance requirements.
  • User Experience: By authenticating users, APIs can also provide personalized experiences, as they know who is making the request and can tailor responses accordingly.

Let's Begin
Let's create a new project

Following the API construction, we will install the nuget package "Microsoft.AspNetCore.Authentication.JwtBearer".

For Authentication, we will be using Okta; Okta provides a way to manage and provide access to users and gives its developer platform to try out authentication stuff.

Sign up on the below link

https://developer.okta.com/signup/

Use authO options, and pick up the region of your choice.
After that, we will create a new API, and In the identifier, we usually give out the hosted app URL, but here for testing purposes. We will give our localhost url of our API.

Now click on the APIs on the left side and click on the Test section; you will get the curl command to generate the Okta auth token.

Now let's open our Program.cs, and we will add the following code.
builder.Services.AddAuthentication(options =>

  {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

  }).AddJwtBearer(options =>

  {
    options.Authority = "https://dev-7fjgp6661wzrvpqw.us.auth0.com/";
    options.Audience = "https://localhost:7294";

  });


From the values in Okta portal, we can use it in the Postman to get the token response.


Now we will add a new controller in our API called "AllowAllController".
[Route("api/[controller]")]
[ApiController]

public class AllowAllController: ControllerBase {
  [HttpGet]
  public string Get() {
    return "Working okay";
  }
}

Once we run the API and hit this endpoint, we will get the response.


Now to test authorization, we will add a [Authorize] keyboard at the top of the controller.

So now our modified controller looks like this.
[Route("api/[controller]")]
[ApiController]
[Authorize]

public class AllowAllController: ControllerBase
{
  [HttpGet]
  public string Get()
  {
    return "Working okay";
  }
}


Now when we hit our API, it will get the Unauthorized response.

Now we will pass the token we get from the token endpoint in the Authorization Header of the request.




About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in