Git Product home page Git Product logo

henrl / aspnet-core-registration-login-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cornflourblue/aspnet-core-registration-login-api

0.0 0.0 0.0 21 KB

ASP.NET Core 2.1 API for User Management, Authentication and Registration

Home Page: http://jasonwatmore.com/post/2018/06/26/aspnet-core-21-simple-api-for-authentication-registration-and-user-management

License: MIT License

C# 100.00%

aspnet-core-registration-login-api's Introduction

JWT Authentication demo using .NET Core 2.1 and PostgreSQL database

ASP.NET Core 2.1 - API for User Management, Authentication and Registration

This is a fork of aspnet-core-registration-login-api, originally created by Jason Watmore.

Please let me know if I have incorrectly credited the original author.

For documentation and instructions check out http://jasonwatmore.com/post/2018/06/26/aspnet-core-21-simple-api-for-authentication-registration-and-user-management

Changes made:

  • Modified code to use a PostgreSQL database instead of the inMemory database.
  • Add a test project (will contain tests soon).

Prerequisites

  • Download the .NET Core 2.2 SDK
  • Download Visual Studio Code
  • Install the C# Extension for Visual Studio Code
  • Download PostgreSQL

Steps taken

  • Create the Auth.Web directory and put the code from the original repo into it.
  • In the main directory, type dotnet new sln --name <INSERT_NAME_HERE>. This should initialise a new solution.
  • Type dotnet sln add ./Auth.Web/WebApi.csproj
  • Make a new directory called Auth.Test and navigate to it.
  • Type dotnet new nunit. This creates an NUnit3 unit test project. NUnit is one of three testing frameworks you can use.

Adding a new package

Integrating with the database (PostgreSQL)

  • Create a new database using pgAdmin called auth_test. Note the connection details for this database. You will need to fill out the DataAccessPostgreSqlProvider field in appsettings.Development.json with these details.
  • Download the package Npgsql.EntityFrameworkCore.PostgreSQL using the dotnet add command.
  • Type dotnet ef migrations add <NAME_OF_MIGRATION>.
  • Check in the migrations folder for the newly created migration. Open the migration file and make any necessary modifications to the schema constraints (if it makes a new table).
  • When you are happy with the migration, type dotnet ef database update.
  • If you need to rollback the database to the previous migration, type dotnet ef database update <PREVIOUS_MIGRATION_NAME>. You will have to delete the bad migration manually.
  • To reverse all migrations made to the database, type dotnet ef migrations 0.

Running the application

  • In the main project directory (not the solution directory), type dotnet watch run. It is best to run dotnet build beforehand to ensure the project compiles successfully. The console should give you the URL endpoint to interact with the application e.g. http://localhost:4000.
  • Open Postman and make a POST request to http://localhost:4000/Users/register with payload body:
{
	"firstname": "Test",
	"lastname": "Test",
	"username": "test",
	"password": "password",
	"bio": "This is a test account"
}

Ensure you have selected the "raw" radio button and the "JSON (application\json)" option.

  • Click "Send". This should register the "test" user with password "password".
  • Send a GET request to http://localhost:4000/Users. You should get a 401 Unauthorised error.
  • Login by sending a POST request to http://localhost:4000/Users/authenticate with payload body:
{
	"username": "test",
	"password": "password"
}

Ensure you have selected the "raw" radio button and the "JSON (application\json)" option. In the response body, you will receive a JWT token. Make a note of this token string.

  • Send a GET request to http://localhost:4000/Users, after selecting the Bearer Token option in the Type dropdown of the Authorization pane. Ensure that you have copied the token string from the previous step into the Token field. This should now show a list of all users in the database.
  • In a similar manner to the previous step, send a GET request to http://localhost:4000/Users/1. If you have included the JWT bearer token, it should show you the details of the user with ID 1.
  • To update a user's details e.g. User 1, send a PUT request to http://localhost:4000/Users/1.

aspnet-core-registration-login-api's People

Contributors

henrl avatar cornflourblue avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.