Skip to content

thiagoloureiro/WebAPIBase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebAPIBase

WebAPI (.NET) Base Project with JWT Auth, Dapper (ORM), SQL Server DB, Crypto, Swagger

Build Status

Build history

Instalation / Configuration Steps:

  1. Clone the repository git clone https://github.com/thiagoloureiro/WebAPIBase.git

  2. Open the Solution file (Visual Studio 2017)

  3. Create a Database (any desired name) in SQL Server, you can use an existing one.

  4. Modify Web.Config file in WebAPI Project, connectionstring:

    <add name="SqlServerConnString" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=DBModel;User Id=user; Password=passwd;MultipleActiveResultSets=True" />
    

Modify using your values.

  1. Run the User table Script (Located in DB Project)
CREATE TABLE [dbo].[User] (
    [Id]             INT            IDENTITY (1, 1) NOT NULL,
    [Name]           VARCHAR (50)   NULL,
    [Surname]        VARCHAR (50)   NULL,
    [Email]          VARCHAR (50)   NULL,
    [Phone]          NCHAR (10)     NULL,
    [LastLogon]      DATETIME2 (7)  NULL,
    [CreatedOn]      DATETIME2 (7)  NULL,
    [ActivationCode] INT            NULL,
    [Login]          VARCHAR (50)   NOT NULL,
    [Password]       VARCHAR (50)   NOT NULL,
    CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC)
);
  1. Run the Project.

  2. After run the project, in the address bar you will have something like: http://localhost:52915/ (the port may change) add swagger to address, for ex: http://localhost:52915/swagger, a Swagger page should be displayed.

  3. You will notice 2 endpoints, api/user and api/user/create, the first one is to login and the second one to create a new user to generate the token for the JWT Authentication.

  4. Access the /api/user/create endpoind and create a new user, you should receive a result "User Created Successfully! :)"

  5. Test Login with the Created User in the /api/user endpoint, with the following request

{
  "username": "UserNameHere",
  "password": "PasswordHere"
}
  1. You Should receive a ReponseBody like this:
{
  "Id": 2,
  "Name": null,
  "Surname": null,
  "Email": null,
  "Phone": null,
  "LastLogon": "2017-09-29T23:00:56.3166667",
  "CreatedOn": "2017-09-29T23:00:56.3166667",
  "ActivationCode": null,
  "Login": null,
  "Password": null,
  "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6InRlIiwibmJmIjoxNTA2NzE4ODk0LCJleHAiOjE1MDY3MjAwOTQsImlhdCI6MTUwNjcxODg5NH0.L5LEVLclhj8MSx4stFO44HYRkkdVwb3Pk_ILejRtqVA"
}
  1. Now you are all set, just create the other Controllers following the patterns and rock on :) Remember to dot use the [AllowAnonymous] from the other Controllers you are going to create, because with this property will ignore and you can access the API Without Authentication.

  2. To use Authentication for example using Postman, in the HEADER add a Key called "Authorization" (without quotes) and Value add: Bearer "the received token from the login" (without quotes)

  3. Any Questions please feel free to ask ! :)

Enjoy!

About

WebAPI (.NET) Base Project with JWT Auth, Dapper (ORM), SQL Server DB, Crypto, Swagger, DI (Unit), UnitTests

Resources

Stars

Watchers

Forks

Packages

No packages published