Example projects for demonstrating Web application securities
- Basic knowledge of C#
- Basic knowledge of JavaScript and HTML
- Basic knowledge of HTTP and Web
To be able to run hands-on excercises, install below listed tools:
- Install Git
- Install Visual Studio Code
- Install C# extension for Visual Studio Code
- Install .NET Core
- Install Chrome
- Install JSONView extension for Chrome
- Install Postman application for Chrome
[to be documented]
Firstly, view and run the BooksOnline.Api web application project
- From terminal (cmd on Windows), navigate to
BooksOnline.Api
folder - Type
code .
from terminal to open the project - Type
dotnet run
from terminal to run the web application - From Chrome browser, enter http://localhost:5000/books
- A list of books (in JSON format) will be displayed in browser
There is no authentication check in the API (http://localhost:5000/books), Now add basic authentication for it:
- Locate file
BooksController.cs
- Add
[Authorize(ActiveAuthenticationSchemes = "Basic")]
attribute to theGet()
method to add Basic authentication for the API - Re-run the application and visit http://localhost:5000/books
- There should be a popup windows asking for credentials
- Open browser's Developer Tools window to check Authorization header from below requests / responses
- Try to input some random invalid credentials, browser should keep asking credentials
- Input the hard-coded credentials
admin:admin
, then the books list should be returned. - For the authentication details, go to
BasicAuthenticationHandler.cs
to view the details.
View and run the BooksOnline web application project
- From terminal (cmd on Windows), navigate to
BooksOnline
folder - Type
dotnet run
from terminal to run the project - From Chrome browser, enter http://localhost:5001
- The home page is displayed
There is no authentication of the home page, now add forms authentication for it:
- Locate file
HomeController.cs
- Add
[Authorize]
attribute at class level - Re-run the application and visit http://localhost:5001
- The page should be redirected to login page
- Open browser's Developer Tools window to check the network traffics from login process
- Input the hard-coded credentials
test@localhost:password
, then home page should be returned. - Read the
Login(LoginViewModel model, string returnUrl = null)
method from fileAccountController.cs
understand how the user is authenticated.
This exercise requires an OpenId Connect authentication service, Azure Active Directory is one of the services.
- Register http://localhost:5001 in an Azure Active Directory instance
- find and uncomment the block
app.UseOpenIdConnectAuthentication
to enable OpenID Connect authentication - Input the registered client ID and authority for the OpendID Connect options
- Add
[Authorize(ActiveAuthenticationSchemes="BooksOnlineCookie")]
attribute toHomeController.cs
as class level - Re-run the application and visit http://localhost:5001
- The page should be redirected to Azure Active Directory login page.