Categories
ASP.Net 5 ASP.Net Core

Hot Reloading in ASP.NET core apps

During the development we often need to make small changes in JavaScript, in CSS or in HTML, to check the change on the browse, we have to compile and run the application in Visual Studio, if app is already running in the browser, then have to stop it and restart the project from VS. I found it quite inconvenient and not productive. Whereas other front end tools like ReactJs, Vue has hot reloading feature with their integrated CLI, as soon as we make changes in the code browser immediately reloads that change.

Now .NET Core has this feature with one simple command

dotnet watch run

use this command from project folder using a cmd prompt, this command will start the app in the browser and will watch for changes in the files and restarts when a change is detected. This feature made development productive and painless.

Happy Coding 🙂

Categories
Azure Virtual Machine

Expose Azure VM services publicly

Sometimes we required to expose service to access from public IP address essentially if the services are running on the virtual machine in Azure. In my case virtual machine was running a Windows 2016 server OS, it was my development machine, where I was developing some web applications. Here are the steps to allow some of the web applications running on virtual machine that need to access publicly.

First need login to your Azure portal, and go to that resource where the service is running, from the left side menu go to Networking tab like the below image –

Once you land on the Networking tab go to the “Add inbound port rule”, at the right side like the image below –

There we need to set new rules to connect to the VM services publicly

First rule source should be allow “Any” to anyone to connect from public domain same follow source port ranges. Destination for the VM can set here, setting for Destination port ranges is important, since my services in VM running on HTTP and the port is 80, so port 80 is open for external access. Protocol should set to TCP. We can set rule name in there which is not editable once set.

Now from any PC the service can be accessed using the VM IP address and the web page that is running on the VM. If the VM IP address is 20.20.20.20 and application URL is http://localhost/portal/login.asp, then you should be allowed to access like http://20.20.20.20/portal/login.asp.

Happy Exploring 🙂




Categories
DLL/Assembly

Register managed DLL in Windows

Registering a managed DLL file can be simple process if you know the correct .NET version. Registering process is slightly different for 32, 64 bit and .NET version. There is no straight way to know .NET version, using a tool like JustCompile is useful to get the .NET version, also shows internal properties and method regarding the DLL.

Once we know the .NET version and 32 or 64 bit variant of the DLL, we need to run windows command prompt in elevated mode. If the .NET version is 2.0 and 32 Bit, run this command below –

C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe <absolutePathToYourDLL.dll> /register /codebase /tlb

To register a 64 bit assembly, simply need to refer to 64 bit framework

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe <absolutePathToYourDLL.dll> /register /codebase /tlb

Above command will generate a .tlb (typeLibFile) and /codebase basically register the assembly globally. Here you can find documentation.

To register a .NET 4 and 32 bit assembly run the command below –

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe <absolutePathToYourDLL.dll> /register /codebase /tlb

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe <absolutePath.dll> /register /codebase /tlb

To unregister .NET 2.0 DLL run below command

C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe <absolutePath.dll> /unregister

In the same way to unregister a .NET 4.0 assembly, run the command below

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe <absolutePath.dll> /unregister

Hope it will help you.

Happy coding 🙂

Categories
ASP.Net Core Identity

Get logged in user name in Core 3.0

To get current logged in user from Identity 2 in .net core 3.0 and 3.1, use the following code.

User.FindFirst(ClaimTypes.Name).Value

Happy Coding 🙂

Categories
ASP.Net Core Identity

ASP.Net Core Seed User and Role

Data seeding for Identity user tables are different. Here is an example code how to seed data without extending the Identity User table. First step to create a class in Data folder Name it “ApplicationDbInitializer.cs”, following code will be in this class.

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AppName.Data
{
    public class ApplicationDbInitializer
    {
        public static void SeedUsers(UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager)
        {
            string admin = "Admin";
            string normalUser = "NormalUser";
            string password = "p@55w0rD"; 

            if (roleManager.FindByNameAsync("Admin").Result == null)
            {
                IdentityRole role = new IdentityRole
                {
                    Name = admin,
                    NormalizedName = "ADMIN"
                };

                roleManager.CreateAsync(role).Wait();
            }

            if (roleManager.FindByNameAsync("NormalUser").Result == null)
            {
                IdentityRole role1 = new IdentityRole
                {
                    Name = normalUser,
                    NormalizedName = "NORMALUSER"
                };

                roleManager.CreateAsync(role1).Wait();                
            }

            if (userManager.FindByEmailAsync("admin@domain.com").Result == null)
            {
                IdentityUser user = new IdentityUser
                {
                    UserName = "admin@domain.com",
                    Email = "admin@domain.com"
                };

                IdentityResult result = userManager.CreateAsync(user, password).Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, admin).Wait();
                }
            }
        }
    }
}

Second step, call above class from Startup.cs file Configure method. Add this line at bottom of the Configure method.

ApplicationDbInitializer.SeedUsers(userManager, roleManager);

Add dependency injection in Configure method arguments like the code below

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager)

Add these two string “UserManager userManager, RoleManager roleManager” in Configure method argument.

In the Startup.cs file need to update ConfigureServices method, create a new service for Identity. Add the following code in ConfigureServices method –

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<AppDbContext>();

Once you run the application, it should create a user, role and assign the user in role in Identity User table.

Happy Coding 🙂

Categories
Git Quick Note To Me

Create git repo from existing project

Often I forget the commands to create git repo from existing project in my local machine.

First step is to init a git repo in your project. Use git bash

$ git init

Second step to add the project files into local git repo

$ git add .

Use the .(dot) after the add, this is saying add all files.

This command is to commit the files with message.

$ git commit -m "Commit message"

Create a repo in the github

$ git remote add origin 'remote repository URL'

After that use following command to push your code to remote repo in the github.

$ git push origin master

After the this command you should be able to see your code in the github.

Happy Coding 🙂