ASP-DotNet Web App On Linux with Authentication

Introduction – ASP-DotNet Web App On Linux

ASP-DotNet Web App On Linux is part of my series on using .NetCore 3 and .Net 5 under Linux with JetBrains Rider. However, this particular guide should work under all operating systems and with all IDE’s.

Using Visual Studio on Windows, the default Asp.NetCore Web app with personal identification uses Microsoft SQL Server and entity framework. Alternatively, when using JetBrains Rider under Linux the default app uses SQLite and Entity Framework. Rather, it is supposed to, however the default application loads with errors. Subsequently, neither of these methods is ideal. Visual studios solution will not work because the connection string provided does not work on SQL Server for Linux. Additionally, comparatively few Linux servers use SQL Server when compared to MySQL and derivatives. Finally, Rider has missing packages and greets the users with lots of red squiggles in a default application.

Getting Started – ASP-DotNet Web App On Linux

Open Rider (or your IDE of choice). Create a new ASP.Net Core Web Application and select individual authentication. If you are using visual studio it is probably also a good idea to choose with run time compilation.

JetBrains Rider Showing - Creating a web app with authentication
JetBrains Rider Showing – Creating a web app with authentication

Add A NuGet Package – ASP-DotNet Web App On Linux

Credit for the NuGet package information goes to S.Ravi Kumar who wrote a similar guide. However, his article focuses on Visual Studio and set up with MVC. First Install one of the following NuGet packages (depending on the needs of the application). The image below is from Rider, but the packages are availible from any Nuget Package manger including the console.

  • Pomolo.EntityFrameWorkCore.Lolita.Sqlite
  • Pomolo.EntityFrameWorkCore.MySql
Pomolo entity framework nuget package
Pomolo entity framework NuGet package

Delete The Default Migrations Folder – If present

The migrations folder may or may not be present depending on how the application was set up initially. However, if it is present, it needs removing as it will not be used. Notably, if the application was created in Rider Under Linux with the above procedure it will not be present. In contrast, if the package was set up using Visual Studio it will be present.

Delete The Default Migrations Folder.

Edit The Startup.cs File – ASP-DotNet Web App On Linux

SQL Server – Startup.cs – Services Method

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddRazorPages();
        }

SQLite – Startup.cs – Services Method

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlite(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddRazorPages();
        }

MySQL – Startup.cs – Services Method

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySql(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddRazorPages();
        }

Edit The Connection String in appsettings.json – ASP-DotNet Web App On Linux

Items with a double asterisk as the start and end should be replaced by a setting unique to the system in use. However also note, that the server name is unlikely to need changing. Subsequently in these cases just remove the asterisks.

SQLite – appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "DataSource=app.db"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

MySQL – appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=**localhost**;Database=**DatabaseName**;Uid=**YourMySqlUserName**;Pwd=**MySqlUserPassword**;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

SQL Server – appsettings.json

This is the default for SQL Server on Linux. In contrast windows users likely want the default setting in Visual Studio. This is because Under Windows SQL Server uses windows authentication by default.

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=**localhost**;Database=**DataBaseName**;User Id=**SA**;Password=**SqlServerUserPassword**;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Do The Entity Framework Migrations – ASP-DotNet Web App On Linux

Either navigate to the project folder in the terminal or use the terminal area inside Rider. Note that the terminal window in Rider opens inside the solution folder. Before doing the migrations change directory into the project folder.

  • dotnet ef migrations add InitialCreate
  • dotnet ef database update
Rider With Terminal Window Open showing entity framework migrations
Rider With Terminal Window Open showing entity framework migrations

Related Articles

Related Post

3 thoughts on “ASP-DotNet Web App On Linux with Authentication

Leave a Reply

Your email address will not be published. Required fields are marked *