Create an Application With Confinity
In this guide we will show you how to create your first Confinity application. In this tutorial we use Confinity with all the default settings and a SQLite database.
What you need
- .Net SDK v5.0 (or newer) installed (Link)
- a cup of coffee or tea
Let's get started
First, create a new MVC application by executing the following command in your desired project location folder.
dotnet new mvc
You can now add Confinity by running
dotnet add package Confinity.WebUI
Now lets register all Confinity services by opening your Startup.cs and call AddConfinity() in the ConfigureServices method.
var builder = WebApplication.CreateBuilder(args);
var confinityOptions = new ConfinityOptions(new SqliteDatabaseProviderConfiguration());
builder.Services.AddConfinity(builder, confinityOptions, moduleBuilder => { });
Also add a new variable _webHostEnvironment in the startup class and set the variable in the Startup Method.
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
_configuration = configuration;
}
In the Configure method, call app.UseConfinity() and endpoints.MapConfinity().
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseConfinity(env);
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapConfinity();
});
}
Last but now least, we have to extend the appsettings.json with the required settings, namely the database connection string and the path to the JWT key file.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Confinity": {
"Database": {
"DbProvider": "Sqlite",
"ConnectionString": "DataSource=./docs.db"
},
"AuthJwk": {
"Filepath": "./authJsonWebKey.json"
}
}
}
To create a new JWT key file you can use a tool like mkjwk.
First login
That's it. You can now start your application and browse to localhost:5000/.confinity. And log in using the username confinity and password confinity.