Skip to content

Angular Single Page Application with an ASP.NET Core Web API that uses token authentication

License

robisim74/AngularSPAWebAPI

Repository files navigation

Angular SPA Web API

Angular v5 Single Page Application with an ASP.NET Core Web API that uses token authentication. The OAuth 2.0 Resource Owner Password Credentials grant (ROPC) is implemented using IdentityServer4 and ASP.NET Core Identity as membership system with a SQLite database.

Get the Changelog.

Live example and its explanation.

Links

ROPC grant requires the use of SSL.

For more complex scenarios, where web services are required by more than one application or third-party applications, you should use an OpenID Connect flow.

Links

The same scenarios are also supported by AspNet.Security.OpenIdConnect.Server and openiddict-core.

Links

Project structure

AngularSPAWebAPI ASP.NET Core Web API project

  • wwwroot Root for Angular application deployment
  • app Angular application
  • Controllers
    • IdentityController.cs Identity APIs
    • ValuesController.cs Resources APIs
  • Data Entity Framework migrations
    • DbInitializer.cs Provides method to populate the db
  • Models
    • ApplicationUser.cs Profile data for application users
  • Config.cs IdentityServer4 configuration
  • IdentityDB.sqlite SQLite database
  • package.json Packages for Angular app
  • Startup.cs Web API configuration
  • tsconfig.json & tsconfig-aot.json TypeScript & ngc compiler options
  • webpack.config.js Webpack configuration file for development & production of Angular app

Installing

  • Requirements
  • Edit ConnectionStrings in appsettings.json

Visual Studio 2017

  • Make sure your configuration for external tools is correct: Tools > Options > Projects and Solutions > Web Package Management > External Web Tools
     .\node_modules\.bin $(PATH) ... 
  • Wait for packages restoring and build the solution

Command line & .NET Core CLI

Go to the AngularSPAWebAPI project folder:

  • npm install
  • Restore & build the solution:
     dotnet restore dotnet build

Running

To run npm commands in Visual Studio you can use NPM Task Runner extension.

Development

For the Angular app, we use JiT compilation, with source maps & Hot Module Replacement.

Visual Studio 2017

  • Start debugging

Command line & .NET Core CLI

Make the changes: the browser will update without refreshing.

Staging / Production

For the Angular app, we use AoT compilation, tree shaking & minification.

Visual Studio 2017

  • npm run build
  • Use AngularSPAWebAPI profile that has Staging as environment variable
  • Start debugging

Command line & .NET Core CLI

  • npm run build
  • Set Staging as environment variable
  • dotnet run

EF Core commands

  • Package Manager Console
     Add-Migration [Name] -OutputDir Data/Migrations Update-Database
  • .NET Core CLI
     dotnet ef migrations add [Name] -o Data/Migrations dotnet ef database update

Changing db

To use another database:

  • Edit ConnectionStrings in appsettings.json
  • Edit in Startup.cs:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

Swagger

  • Navigate to http://localhost:5000/swagger/v1/swagger.json to see the document generated that describes the endpoints
  • Swagger UI can be viewed by navigating to http://localhost:5000/swagger

To test the APIs, remove the policy from controllers.

License

MIT

About

Angular Single Page Application with an ASP.NET Core Web API that uses token authentication

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •