Adding authentication and authorization flow

This commit is contained in:
2025-02-26 22:23:15 +01:00
parent 176f149be3
commit 76779afd2e
22 changed files with 562 additions and 19 deletions

View File

@@ -21,6 +21,21 @@
"Name": "MIT License",
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"JWTSettings": {
"ValidAudience": "http://localhost:4200",
"ValidIssuer": "http://localhost:5000",
"Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr",
"ExpiredAfterMinsOfInactivity": 15
},
"EncryptionSettings": {
"Salt": "S7VIidfXQf1tOQYX"
}
}

View File

@@ -0,0 +1,42 @@
{
"AppSettings" :
{
"Settings": {
"Name": "MainProject",
"Version": "v1.0",
"Description": "This template contains basic configuration for a .Net 8 backend"
},
"DatabaseSettings": {
"SqlServerConnectionString": "SQLSERVER_DB_SERVER",
"MongoDbConnectionString": "MONGO_DB_SERVER",
"PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER"
},
"OpenApiSettings": {
"TermsOfServiceUrl": "",
"OpenApiContact": {
"Name": "",
"Url": ""
},
"OpenApiLicense": {
"Name": "MIT License",
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"JWTSettings": {
"ValidAudience": "http://localhost:4200",
"ValidIssuer": "http://localhost:5000",
"Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr",
"ExpiredAfterMinsOfInactivity": 15
},
"EncryptionSettings": {
"Salt": "AAAAA"
}
}
}

View File

@@ -0,0 +1,108 @@
using System;
using BasicDotnetTemplate.MainProject.Models.Settings;
using Microsoft.AspNetCore.Builder;
using BasicDotnetTemplate.MainProject.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class CryptoUtils_Tests
{
[TestMethod]
public void Decrypt_Success()
{
try
{
string encryptedData = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
CryptUtils cryptoUtils = new CryptUtils(appSettings);
var decryptedData = cryptoUtils.Decrypt(encryptedData);
var isEqual = decryptedData == "ThisIsASuccessfullTest";
Assert.IsTrue(isEqual);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
}
}
[TestMethod]
public void Decrypt_Error()
{
try
{
string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
CryptUtils cryptoUtils = new CryptUtils(appSettings);
var decryptedData = cryptoUtils.Decrypt(encryptedData);
var isEqual = decryptedData == "ThisIsASuccessfullTest";
Assert.IsFalse(isEqual);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
}
}
[TestMethod]
public void Decrypt_ArgumentException()
{
try
{
string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "invalidCryptAppsettings.json");
CryptUtils cryptoUtils = new CryptUtils(appSettings);
try
{
var decryptedData = cryptoUtils.Decrypt(encryptedData);
Assert.Fail($"Expected exception instead of response: {decryptedData}");
}
catch (ArgumentException argumentException)
{
Assert.IsInstanceOfType(argumentException, typeof(ArgumentException));
}
catch (Exception exception)
{
Assert.IsInstanceOfType(exception, typeof(ArgumentException));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
}
}
[TestMethod]
public void Decrypt_Empty()
{
try
{
string encryptedData = "WGHWRBE5mVWaV=";
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
CryptUtils cryptoUtils = new CryptUtils(appSettings);
var decryptedData = cryptoUtils.Decrypt(encryptedData);
var isEqual = decryptedData == String.Empty;
Assert.IsTrue(isEqual);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
}
}
}

View File

@@ -1,11 +1,3 @@
using System;
using System.Reflection;
using System.Net;
using System.Net.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BasicDotnetTemplate.MainProject;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using Microsoft.Extensions.DependencyModel.Resolution;
using BasicDotnetTemplate.MainProject.Models.Settings;
using Microsoft.AspNetCore.Builder;
using BasicDotnetTemplate.MainProject.Utils;