ASP.NET MVC Redirect to Action from a Class

var context = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
                var rc = new System.Web.Routing.RequestContext(context, new System.Web.Routing.RouteData());
                var urlHelper = new UrlHelper(rc);
                context.Response.Redirect(urlHelper.Action("login", "account"), false);

Cannot load MVC4 Web project in VS 2017 or VS 2019

I have a web project, it could load fine in VS15, but in VS2019 it’s failing, saying the project is incompatible. The migration report contains the following message: ========== .csproj: The application which this project type is based on was not found. Please try this link for further information:

To resolve it:

1. Open a .csproj file using a text editor such as Visual Studio Code or notepad

2. Find the code <ProjectTypeGuids> {E3E379DF-F4C6-4180-9B81-6769533ABE47}; {349c5851-65df-11da-9384-00065b846f21}; {fae04ec0-301f-11d3-bf4b-00c04f79efbc} ProjectTypeGuids>

3. Delete the code section {E3E379DF-F4C6-4180-9B81-6769533ABE47}  and save the file.

4. Re-open the solution.

Missing Windows Authentication Feature IIS and Windows 10

If you have windows 10 Home or SL version when might be you are unable to see Windows Authentication under IIS options when installing.

To enable “Windows Authentication” either you need to upgrade the OS to Pro version or you can run this command to enable it.

C:\WINDOWS\system32>dism /online /norestart /add-package:%SystemRoot%\servicing\Packages\Microsoft-Windows-IIS-WebServer-AddOn-2-Package~31bf3856ad364e35~amd64~~10.0.17134.1.mum

The file name will be different based on windows updates. So please update if requires.

How to Backup Microsoft SQL Server (MSSQL) Databases

--1. Variable declaration

DECLARE @filename VARCHAR(256)
-- 2. Setting the backup path

SET @path = 'X:\MSSQLDB\Backup\'  

SELECT name 
FROM master.dbo.sysdatabases 
WHERE name NOT IN ('master','model','msdb','tempdb')  -- ignore system databases

--3. Initializing cursor operations

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   


  -- 4. Defining the filename
    SET @fileName = @path + @name + '.BAK'  
    BACKUP DATABASE @name TO DISK = @fileName 

    FETCH NEXT FROM db_cursor INTO @name   
CLOSE db_cursor   
DEALLOCATE db_cursor


How to Create Multiple SQL Server Instances on Same Server

When you install SQL Server, the installation creates an instance (DEFAULT OR SQLEXPRESS) of SQL Server. In my case I have created instanace named DEVSQL.

Some times we do need to create multiple instances based on requirements. To create more instances, start the setup again. The following screen appears when setup starts for the SQL Server 2012.

Click “New SQL Server stand-alone installation or add features to an existing installation”, the following screen appears:

Installation Start

As you can see DEVSQL instance already exists, it was created by the first installation, now select “New installation or add shared features”. Then click Next and accept the License Agreement.

Then select features you want to install for the new instance.

After that the installation offer you to specify the name of your new instance (MYTEST was chosen in this example):

Click Next to create the new instance and perform the remaining setup steps.

Now you are done!


How to get list of all always encrypted columns in SQL Server

We do have “Always encrypted” feature in SQL 2016 and later versions, from this feature we can encrypt the column data instead of encrypting whole database.  Sensitive data like credit card numbers, SSN.
We need to prepare a list of encrypted columns at some where to track for future.
Without tracking anywhere in the documents we can run the simple SQL query which returns all the columns in respective tables and encryption type.

SELECT AS TableName, AS ColumnName, AS KeyName,
FROM sys.columns c
INNER JOIN sys.column_encryption_keys k ON c.column_encryption_key_id = k.column_encryption_key_id
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE encryption_type IS NOT NULL



SQL Server – List all the Constraints by Table or by Column Name

List all Constraints of the Database:

SELECT OBJECT_NAME(object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
type_desc AS ConstraintType
FROM sys.objects

List Constraints  on table column:

 Select SysObjects.[Name] As [Contraint Name] ,Tab.[Name] as [Table Name],Col.[Name] As [Column Name]
From SysObjects Inner Join (Select [Name],[ID] From SysObjects Where XType = 'U') As Tab
On Tab.[ID] = Sysobjects.[Parent_Obj]
Inner Join sysconstraints On sysconstraints.Constid = Sysobjects.[ID]
Inner Join SysColumns Col On Col.[ColID] = sysconstraints.[ColID] And Col.[ID] = Tab.[ID]
WHERE Tab.[Name] ='Employee' AND Col.[Name]='DOB'
order by Col.[Name]

The provided anti-forgery token was meant for user “”, but the current user is “”

While working with MVC, I got HttpAntiForgeryException exception (The provided anti-forgery token was meant for user “”, but the current user is “”) at login page. It happens when user login with valid credentials and re-directed to inner page ofr the application. At inner pages he press the browser back button and will show login page again. He entered the valid credentials again that time, this exception will generate and display on the browser.

To fix this exception: Need to add [OutputCache(NoStore=true, Duration = 0, VaryByParam= “None”)] line to your login get method.

[OutputCache(NoStore=true, Duration = 0, VaryByParam= "None")]
public ActionResult Login(string returnUrl)
ViewBag.ReturnUrl = returnUrl;
return View();

What is tuple and when to use

A tuple is a data structure that has a specific number and sequence of elements. It allow us to combine multiple values with similar or different types into single object without creating a custom class. It can also be used where we have to return multiple items from the method instead of “out” keyword.
From MSDN:

Tuples are commonly used in four ways:

  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
  • To provide easy access to, and manipulation of, a data set.
  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
  • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.