Monday, December 30, 2019

Index Range C#


C# 8 introduced a new syntax for expressing a range of values using 2 System objects namely "System.Index and System.Range". 

They are useful to index/slice C# collections during the runtime of an application.

System.Index

New Index expression of C#, refers "from the end".   This feature introduces a new unary prefix hat(^) operator with the better way to index a collection from the end.

General code syntax is represented as
System.Index operator ^(int fromEnd);

Sample C# Index code is
var lastItem = array[^1];

The above code is equivalent to the tradition code of
var lastItem = array[collection.Count-1]; 

System.Range

Ranges way to access ranges / slices of any collections with an introduction of new range operation ..

The starting index of a range is inclusive, and the ending index is exclusive. Alternatively, the ending can be specified as an offset from the end.

System.Range operator ..(Index start = 0, Index end = ^0);

Sample C# Range code is
var lastItem = array [^1];
var subCollection = array[2..^5];


Above code is equivalent to the traditional way of C# code as
var lastItem = array [array.Count -1];
var subCollection = array.ToList().GetRange(2, 4);


This will help you to avoid LINQ and making your code compact and more readable.

Friday, December 13, 2019

AntiForgeryToken


Cyber Security is a challenging business problem to solve.  Recently, I created the foundation for telematics product to gain the customer confidence.

As .NET engineer, I was looking for out of box solution from Microsoft and here you go 'AntiForgeryToken' With open source culture of .NET Core, it is publicly available at https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1297

By design, AntiForgeryToken generates a hidden form field of anti-forgery token to validate the submitted form. The anti-forgery token can be used to help protect your application against cross-site request forgery.

Let us see the code sample.  It's quite simple to implement by just adding the ValidateAntiForgeryToken attribute to your methods. Flip side is the cost of checking for the token with every request, not just with the HttpPost methods.

[HttpPost]  
[ValidateAntiForgeryToken]  
public ActionResult CreateSensor(Sensor sensor)  
{
  if (ModelState.IsValid)  
  {
    //business logic here...
  }
  return View(ModelName);
}

Alternative solution is to add AutoAntiForgeryToken to the controller classes as below

[AutoAntiForgeryToken]
public class TelematicsDeviceController : Controller
{
   //core logic here...
}

By doing so, the attribute checks only the dangerous methods i.e. only methods that aren't a GET or methods never use TRACE, OPTIONS and HEAD

Happy Cyber Security Coding Fix !!

Tuesday, December 3, 2019

Alphabet CEO change


Alphabet-Google's parent firm CEO Larry Page will step down from the role and Google CEO Sundar Pichai will take over, adding to his current responsibilities. Co-founder Sergey Brin will also step down as president of Alphabet and the role will be eliminated.

Page became CEO of Alphabet after Google restructured to form the parent company in 2015. He had previously been CEO of Google.

Both Page and Brin will remain “actively involved” as members of Alphabet’s board, according to the letter. The co-founders still have controlling voting shares of the company. Page holds about 5.8% of Alphabet shares, Brin controls about 5.6% and Pichai holds about 0.1%, ensuring the new CEO may still be challenged by the company’s founders. Google said its voting structure is not changing in light of the announcement.

Alphabet’s stock was up as much as 0.8% after hours. Page and Brin said in a blog post that “it’s the natural time to simplify our management structure.”