<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nirmal Krishna</title>
    <description>The latest articles on DEV Community by Nirmal Krishna (@meuequalsd).</description>
    <link>https://dev.to/meuequalsd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F444181%2F3b41abe2-d3df-48db-8a60-825b4db0d6e0.png</url>
      <title>DEV Community: Nirmal Krishna</title>
      <link>https://dev.to/meuequalsd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meuequalsd"/>
    <language>en</language>
    <item>
      <title>Finding element that appears once in an array where other elements appear twice : Leetcode</title>
      <dc:creator>Nirmal Krishna</dc:creator>
      <pubDate>Thu, 01 Jul 2021 03:56:16 +0000</pubDate>
      <link>https://dev.to/meuequalsd/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-n91</link>
      <guid>https://dev.to/meuequalsd/finding-element-that-appears-once-in-an-array-where-other-elements-appear-twice-n91</guid>
      <description>&lt;p&gt;This is an example implementation using hashmap. The input array nums is considered to have only one unique number found once, other numbers occurs &amp;gt; once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ts
function singleNumber(nums: number[]): number {

    const hash = {};

    for(let i = 0; i&amp;lt; nums.length; i++){   
        hash[nums[i]] = hash[nums[i]] ? hash[nums[i]] + 1 : 1
    }

    return Object.keys(hash).filter(k=&amp;gt; hash[k] === 1).map(k=&amp;gt; parseInt(k))[0];

};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
      <category>typescript</category>
      <category>arrays</category>
    </item>
    <item>
      <title>ASP.NET Core Identity User Locked out</title>
      <dc:creator>Nirmal Krishna</dc:creator>
      <pubDate>Thu, 24 Jun 2021 15:20:39 +0000</pubDate>
      <link>https://dev.to/meuequalsd/asp-net-core-identity-user-locked-out-3bb0</link>
      <guid>https://dev.to/meuequalsd/asp-net-core-identity-user-locked-out-3bb0</guid>
      <description>&lt;p&gt;The user lockout feature is the way to improve application security by locking out a user that enters a password incorrectly several times. This technique can help us in protecting against brute force attacks, where an attacker repeatedly tries to guess a password. ⛳ &lt;/p&gt;

&lt;p&gt;Quite a basic feature for an authentication service, but adding it in my Web API app was quite a head scratcher.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Adding to configuration service
&lt;/h4&gt;

&lt;p&gt;In your startup.cs or container configuration file,   the config for locking out can be  set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services.AddIdentity&amp;lt;User, IdentityRole&amp;gt;(opt =&amp;gt;
{
    // previous code removed for clarity reasons
    opt.Lockout.AllowedForNewUsers = true;
    opt.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(2);
    opt.Lockout.MaxFailedAccessAttempts = 3;
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The property names are self explanatory here.&lt;/p&gt;

&lt;p&gt;The  above code will  by default setup locking out feature and if a person is trying to login with wrong &lt;code&gt;password&lt;/code&gt; for a given &lt;code&gt;username&lt;/code&gt; the user account will be locked out for 5 minutes updated accordingly in &lt;code&gt;LockoutEnd&lt;/code&gt; column &lt;/p&gt;

&lt;h4&gt;
  
  
  2. How to check if this user is Locked Out?
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//  AuthService.cs
..
    var result = await signInManager.CheckPasswordSignInAsync(user, model.Password, lockoutOnFailure: true);
..
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The properties  of &lt;code&gt;result : SignInResult&lt;/code&gt; we are concerned here are &lt;code&gt;Succeeded&lt;/code&gt;, &lt;code&gt;IsLockedOut&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;✅ &lt;code&gt;Succeeded ==  true&lt;/code&gt; if the username and password &lt;strong&gt;match&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ &lt;code&gt;Succeeded == false&lt;/code&gt; if the username and password &lt;strong&gt;do not match&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;code&gt;IsLockedOut == true&lt;/code&gt; if this user has been locked out after x number of trials&lt;/p&gt;

&lt;h4&gt;
  
  
  3. 🤔 What is here to scratch your hear for
&lt;/h4&gt;

&lt;p&gt;I was expecting &lt;code&gt;LockOutEnabled&lt;/code&gt; will become &lt;code&gt;true (1)&lt;/code&gt; in the identity user table. It took me few hours to get to the documentation but it was stated clearly in the Library class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Microsoft.AspNetCore.Identity.IdentityUser

// Gets or sets a flag indicating if the user could be locked out.

public virtual bool LockoutEnabled { get; set; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I missed the &lt;strong&gt;could be&lt;/strong&gt;  and  it costed me some hours.😪&lt;/p&gt;

&lt;p&gt;Updating this column to &lt;code&gt;true&lt;/code&gt; for necessary users then locks out the user for particular a time limit set in the &lt;code&gt;config&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The proper logging and an error can be thrown with &lt;br&gt;
&lt;code&gt;result.IsLockedOut&lt;/code&gt; flag from the service layer 😅&lt;/p&gt;

&lt;p&gt;📚 References&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;a href="https://stackoverflow.com/questions/53854051/usermanager-checkpasswordasync-vs-signinmanager-passwordsigninasync"&gt;UserManager.CheckPasswordAsync versus SignInManager.PasswordSignInAsync&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://code-maze.com/user-lockout-aspnet-core-identity/"&gt;User Lockout with ASP.NET Core Identity&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>dotnet</category>
      <category>identity</category>
      <category>auth</category>
    </item>
    <item>
      <title>Importance of a Praise folder</title>
      <dc:creator>Nirmal Krishna</dc:creator>
      <pubDate>Fri, 04 Dec 2020 07:12:50 +0000</pubDate>
      <link>https://dev.to/meuequalsd/importance-of-a-praise-folder-5bad</link>
      <guid>https://dev.to/meuequalsd/importance-of-a-praise-folder-5bad</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Create a folder called &lt;strong&gt;Praise&lt;/strong&gt; in your inbox.&lt;/li&gt;
&lt;li&gt;Remember to move appreciation, &lt;code&gt;job well done&lt;/code&gt; emails to that folder. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keeping a praise folder in your email inbox is essential, it serves a number of purposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It acknowledge your successes and keep track of them&lt;/li&gt;
&lt;li&gt;Gives a morale boost when you read it in future&lt;/li&gt;
&lt;li&gt;Comes handy during a performance review to talk about accomplishments &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>career</category>
    </item>
    <item>
      <title>Querying JSON and its nested objects with TSQL</title>
      <dc:creator>Nirmal Krishna</dc:creator>
      <pubDate>Wed, 18 Nov 2020 17:42:00 +0000</pubDate>
      <link>https://dev.to/meuequalsd/querying-json-and-its-nested-objects-with-tsql-1cif</link>
      <guid>https://dev.to/meuequalsd/querying-json-and-its-nested-objects-with-tsql-1cif</guid>
      <description>&lt;p&gt;When we were faced to store lot of dynamically modelled data in a relational DB, we decided on storing them in a JSON column of SQL server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/json-functions-transact-sql?view=sql-server-ver15" rel="noopener noreferrer"&gt;The well documented&lt;/a&gt; recourses came in handy during initial development.&lt;/p&gt;

&lt;p&gt;Although with some workaround&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runtime JSON deserialization&lt;/li&gt;
&lt;li&gt;Getting around with Store procedures while whole application runs with entity framework&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Until today I never got to query nested arrays of a JSON object with SQL. That is when I stumbled upon another method in T-SQL called &lt;strong&gt;&lt;code&gt;OPENJSON&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;OPENJSON is a table-valued function that parses JSON text and returns objects and properties from the JSON input as rows and columns.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, OPENJSON provides a rowset view over a JSON document. You can explicitly specify the columns in the rowset and the JSON property paths used to populate the columns. Since OPENJSON returns a set of rows, you can use OPENJSON in the FROM clause of a T-SQL statement just as you can use any other table, view, or table-valued function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Few samples
&lt;/h4&gt;

&lt;p&gt;Consider this sample json data&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

DECLARE @json NVARCHAR(MAX) = '{
    "firstName":"James",
    "lastName":"Doe",
    "ChildrenDetails":[
        {
            "Id":1,
            "Name":"John Doe",
            "Languages":["English", "German"]
        },
        {
            "Id":2,
            "Name":"Jane Doe",
            "Languages":["English"]
        },
        {
            "Id":3,
            "Name":"June Doe",
            "Languages":["German", "Tamil"]
        }]
}'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  1. Querying into arrays (ChildrenDetails)
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

SELECT 
    JSON_VALUE(d.value,'$.Id') AS Id,
    JSON_VALUE(d.value,'$.Name') AS Name
FROM OPENJSON(@json,'$.ChildrenDetails') AS d


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr4wq978smffpc49l31pn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr4wq978smffpc49l31pn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Flatten Nested arrays (ChildrenDetails + each language)
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

SELECT 
JSON_VALUE(d.value,'$.Id') AS Id,
JSON_VALUE(d.value,'$.Name') AS Languages,
l.value AS Name
  FROM OPENJSON(@json,'$.ChildrenDetails') AS d CROSS APPLY OPENJSON (d.value,'$.Languages') AS l


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn6g3426psj8m055t0gy0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn6g3426psj8m055t0gy0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully you can try this with complex JSON structures. The use cases are many, but today this came in handy for a feature to us 😃&lt;/p&gt;

&lt;p&gt;Before you leave, a meme:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu577z2v5n8z2fssw2n5c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu577z2v5n8z2fssw2n5c.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EOF&lt;/code&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>todayilearned</category>
      <category>database</category>
    </item>
    <item>
      <title>I learnt to shuffle an array and an important lesson. </title>
      <dc:creator>Nirmal Krishna</dc:creator>
      <pubDate>Tue, 04 Aug 2020 17:14:28 +0000</pubDate>
      <link>https://dev.to/meuequalsd/i-learnt-to-shuffle-a-array-and-an-important-lesson-2lc7</link>
      <guid>https://dev.to/meuequalsd/i-learnt-to-shuffle-a-array-and-an-important-lesson-2lc7</guid>
      <description>&lt;p&gt;😏All these years I have faced the constant problem of sorting an array of int, strings, by property value. &lt;/p&gt;

&lt;p&gt;😳I have never thought how can I shuffle this sorted array (even unsorted array).&lt;/p&gt;

&lt;p&gt;I came across this problem while I was building a Duolingo style Math quiz app. I had to randomize the position of the correct answer in the &lt;em&gt;array&lt;/em&gt; of options. &lt;/p&gt;

&lt;p&gt;🤔 I pondered over writing some pseudo code for it, I knew it has to do with randomly selecting an index between the boundaries of the array.&lt;/p&gt;

&lt;p&gt;😴 Yet as a &lt;em&gt;lazy&lt;/em&gt; engineer I googled for it immediately, to see how people do it. I came across &lt;a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm"&gt;Fisher–Yates shuffle&lt;/a&gt; algorithm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i from n−1 downto 1 do
     j = random integer such that 0 ≤ j ≤ i
     exchange a[j] and a[i]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🤯 It was quite intriguing with its simplicity. I was able to shuffle my options array. &lt;br&gt;
But then I thought, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Would I have come up with similar algo. without google? I do not know but it would have taken some time&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;😇 I will not be able to solve all problems without taking help. But I should be comfortable to try and solve it, before getting answers from google. &lt;/p&gt;

&lt;p&gt;I had to practice on solving problems that make me uncomfortable.&lt;/p&gt;

&lt;p&gt;🚩 I need to take on new challenges outside of my fulltime work(codewars, Leetcode, Freecodecamp, &lt;a href="https://twitter.com/hashtag/100DaysOfCode?"&gt;#100daysofreact&lt;/a&gt;)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The experience of reinventing the wheel is different for every person&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;The implementation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shuffleOptions = (optionsArr) =&amp;gt; {
        var m = optionsArr.length, t, i;
        while (m) {
            i = Math.floor(Math.random() * m--);
            t = optionsArr[m];
            optionsArr[m] = optionsArr[i];
            optionsArr[i] = t;
        }
        return optionsArr;
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Amazing visualization + complexity comparison &lt;a href="https://bost.ocks.org/mike/shuffle/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Randomness mentioned here are pseudo randomness&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>ds</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
