This is a sample ASP.NET Core Web API project that exposes both classic Web API and WebSockets endpoints to process API requests in JSON format from client. It uses Microsoft Azure Database for PostgreSQL as its datasource and Entity Framework Core for its data operations such as user registration and authentication, session management, and verification code system.
- ASP.NET Core 5.0 Web API
- Configured for HTTPS
- Enabled Docker Support (Windows)
- Enabled OpenAPI support
- Raw WebSocket protocol
- MSTest Unit Testing
- Built-in Swagger Support
- Azure Database for PostgreSQL v11
- Microsoft Entity Framework Core (PostgreSQL)
- SHA-256 HMAC hashing
- BackendTest-WebAPI - main web api project
- BackendTest-UnitTest - unit testing project to test web api functions
- _BaseClass.cs - shared methods used among all test classes
- 1-5*Test.cs - unit testing classes for web api
- 6-WebSocketTest.cs - unresolved. Supposed unit testing for web socket
- 6-WebSocketTest.html - unit testing for web socket using javascript
- MSTest Unit-Testing for Web API - on visual studio, press
Ctrl+R, A
or go toTest
menu, then clickRun All Test
item - Swagger for Web API - on visual studio, press
F5
or go toDebug
menu then clickStart Debugging
item - Web Client (Javascript) for Web Socket - on
BackEndTest-UnitTest
project, open6-WebSocketTest.html
on your web browser - Use Third-Party WebSocket Test Client for Chrome - install
WebSocket Test Client
extension found here in Chrome Web Store
/* Client Request */
{
"command": "emailVerification",
"email": "[email protected]",
"username": "johndoe"
}
/* Server Response */
{
"command": "emailVerification",
"success": true,
"remarks": null
}
/* Remarks will contain reason when success returns false */
- AuthenticateController.cs - generate login salt
- AvailableController.cs - checks if username or email has already been used
- BaseController.cs - shared methods across all controllers
- HashController.cs - generate sha 256 hmac hash of text and key
- LoginController.cs - login user and details
- RegisterController.cs - register new user to database
- VerifyController.cs - generate verification code
- WebSocketExtension.cs - add middleware as service to project
- WebSockHandler.cs - socket connection and events handler
- WebSocketmanager.cs - sockets connections manager
- WebSocketMiddleware.cs - implements sockets management and events
- WebSocketRequestHandler.cs - handles requests and response to/from clients
- User - where registered users are stored
- Authentication - stores login salt requests and validity
- Login - successful logins history
- Verification - stores verification codes and validity
- base_address - actual url path to web api
- https_port - port number for https connections
- superSecretKey - secret key to further hash stored passwords
- salt_expiry - login salt expiration in seconds
- session_expiry - login session expiration in seconds
- verification_expiry - verification code expiration in seconds
- verification_max - maximum verification code requests per day