Category Archives: ASP .NET

ASP.NET Core 2.0 Disable Authentication in Development Environment

This article describes how to disable authentication for ASP.NET Core 2.0.

ASP.NET Core JWT Authentication

I have some Rest API which I want to protect via JwtBearer token in production e.g.

 

However in order to test the API, for development I totally want to disable authentication without having to remove all Authorizes in the Controllers.

Continue reading

Hangfire Dashboard and ASP .NET 5

This article demonstrates how the Hangfire dashboard can be set up in an ASP .NET 5 application including authentication via ASP .NET Identity.

Adding Hangfire Packages

project.json:

Since I am using Hangfire with SQL, I have to add Hangfire itself and Hangfire.SqlServer to the project.json.

Continue reading

302 Redirect VS 401 Unauthorized – ASP .NET 5 Identity

302 API Redirect Problem

If you make a request to a REST service and are not logged in, by default ASP .NET 5 Identity returns a 302 Redirect to login page. This is great if you are visiting the URL of a controller directly. If you make an Ajax request to a specific REST API and are not logged in however, you are also redirected to the login page and instead of the content of the API you will get the content of the login page itself. Probably without even noticing it right away.

So in instance of API calls I would prefer a 401 Unauthorized status code, e.g. $http requests using AngularJS. Here only the Ajax call itself gets redirected, but the URL of the browser remains the same. This means you won’t see that there is an “authentication problem” and you are not redirected to the login page.

Continue reading

ASP .NET 5 Identity with Entity Framework 7 Setup

In this article we will demonstrate how to provide authentication to ASP .NET 5 applications using ASP .NET Identity with Entity Framework 7.

Adding Packages

Add the following package to your ASP .NET project. This will automatically pull in additional required dependencies for ASP .NET Identity.

project.json

Continue reading

Dependency Injection in ASP .NET 5 MVC 6

With ASP .NET 5 MVC 6 you do not have to rely on external Dependency Injection (DI) libraries any more. It comes with it’s on onboard DI. I will now demonstrate how to set up Dependency Injection for using Entity Framework 7 DbContexts within Controllers.

Register Service

For simplicity of this example I will not create a separate interface for my DbContext or implement a repository pattern for accessing the database. In bigger projects, I strongly recommend to do so, however. If you want to keep it simple like me you can directly register the DbContext for later on injection into Controllers in the class Startup.cs.

Continue reading

Code First Migrations Entity Framework 7

Create Initial Code First Migration

In order to do Code First Migrations, you first have to set up the command dnx ef. Code first means, that you first do database model changes in the Entity Framework database classes and then apply the changes to the actual database. Please read the 2 previous blog entries about ASP .NET 5 on how to set up the dnx command line tool.

To be more specific, the examples in this article were done using Entity Framework 7 RC1. If you execute dnx ef in your ASP .NET project folder, you will get the following output.

Continue reading

Entity Framework 7 Scaffold DbContext from Existing Database

In previous to 7 versions of the Entity Framework all Entity Framework related commands could be executed within Visual Studio. Now you have to use the new ASP .NET 5 dnx command instead.

DbContext Model Migration from Entity Framework 6

In order to migrate my model from Entity Framework 6 to Entity Framework 7, I did not want to manually adjust my old DbContext for Entity Framework 7. Instead I used scaffolding from my existing database to create a completely new model. This minimizes the risk of introducing errors in the new model. After I generated the model, I deleted the previous Migration History table in the database. This might not be suitable for everyone, but I wanted to make a clean cut after transitioning to EF 7.

Scaffold DbContext

In order to scaffold a DBContext from an existing database, you first have to set up dnx ef. If you have not done so already, please read the 2 previous blog entries about ASP .NET 5.

To be more specific, the examples in this article were done using Entity Framework 7 RC1. If you execute dnx ef in your ASP .NET project folder, you will get the following output.

Continue reading

ASP .NET 5 with Entity Framework 7

Adding Entity Framework 7 Packages to ASP .NET Project

Older versions of the Entity Framework need a full .NET runtime in order to work. If at some point you want to use the new .NET 5 Core e.g. to run your application on Linux etc., you should use Entity Framework 7.

project.json:

A few comments regarding my project dependency setup. Of course, first of all you have to add the Entity Framework libraries as dependencies.

Continue reading

ASP .NET 5 Toolchain Setup with Visual Studio 2015

During the Visual Studio installation include ASP .NET and the Microsoft Web Developer Tools

web-dev-tools

Create a new ASP .NET 5 project using Visual Studio

  1. File -> New -> Project
  2. From the templates select ASP .NET Web Applicationasp1
  3. In the next step select one of the ASP .NET 5 templatesasp2

After clicking OK the new ASP .NET project is created. This will also download and initiate the new .NET toolchain.

Continue reading