URL Shortener is an open-source web service for shortening URLs and based on .NET 6 and Blazor Server techologies.
URL shortener consist of two parts:
- User area
- Admin area
User area is a simple page that working as redirection mechanism.
- User type shortened link in browser
- Goes to the redirector's page (file
Redirector.razor
) Redirector.razor
redirect user to target URL
Admin area has functionality described bellow:
- Authentication flow using Auth0
- Reducing links into short link wit 7-simbolic auto-generated or predefined alias
- Editing target URL in created short link
- Support multilanguage interface (English, Russian, Belarusian)
Also Shortener collect data about rederections for each link: count of redirections, agent info etc.
- Prepare a SQL-like database.
- Setup an SQL Server or any other SQL-like database (for example
Azure SQL Edge
good one for ARM processers and so on) - Create the database with
pds
(or any other name on your choise). - Update the database connection string in the
ConnectionStrings:DefaultConnection
field ofshortener/Shortener/Itbeard.Shortener/appsettings.json
file. - Migration of the database to the current state will occur automatically the first time you run the project.
- Setup an SQL Server or any other SQL-like database (for example
- Set up authentication via Auth0.
- Register an account on Auth0 service.
- Create application
Application
with typeSingle Page Application
- Update shortener settings in
shortener/Shortener/Itbeard.Shortener/appsettings.json
file:Auth0:Domain
= value ofDomain
field from Auth0 applicationAuth0:ClientId
= value ofClient ID
field from Auth0 applicationAuth0:ClientSecret
= value ofClient Secret
field from Auth0 application
- Update settings of Auth0 application:
- In field
Allowed Callback URLs
sethttps://localhost:5001//admin/callback
. - In field
Allowed Logout URLs
sethttps://localhost:5001
.
- In field
- In Auth0, delete the database on the
Authentication - Database
tab or remove yourApplication
link to this database, so that you only authenticate through Google. You can control the login methods on theConnections
tab of your application in Auth0. - Add a
Rule
in which you explicitly specify which emails have access to authorization in the application:- Go to
Auth Pipeline - Rules
menu. - Add rule in accordance with
Whitelist
template and update rule by emails that are allowed to access the authorization. - Or create a rule with the following code (if the
Whitelist
template is suddenly not founded in Auth0):
- Go to
function userWhitelist(user, context, callback) {
// Access should only be granted to verified users.
if (!user.email || !user.email_verified) {
return callback(new UnauthorizedError('Access denied.'));
}
const whitelist = [ '[email protected]', '[email protected]' ]; //authorized users
const userHasAccess = whitelist.some(
function (email) {
return email === user.email;
});
if (!userHasAccess) {
return callback(new UnauthorizedError('Access denied.'));
}
callback(null, user, context);
}
- Add URL of your Shortener instanse in to field
AppUrl
inshortener/Shortener/Itbeard.Shortener/appsettings.json
file. - Build and run project
Itbeard.Shortener
. - Open https://localhost:5001/admin in your browser - this URL is the entry point to the admin interface of the Shortener.
- Instructions of how to setting up Auth0 in Blazor: https://auth0.com/blog/securing-blazor-webassembly-apps/