Skip to content

Commit 83e38e2

Browse files
Merge pull request #123 from kolappannathan/dev
For v7
2 parents 82969ce + 5cd114a commit 83e38e2

File tree

10 files changed

+77
-55
lines changed

10 files changed

+77
-55
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44

55
## [Unreleased]
66

7+
## [7.0.0] - 2023-02-27
8+
### Added
9+
- CORS settings
10+
11+
### Changed
12+
- Updated .NET version to 7
13+
- Using Serilog.AspNetCore for logging instead of serilog extensions as recommended by Serilog
14+
- Log files are now separated by date
15+
- Using new syntax for Argument null checks
16+
- Updated dependencies
17+
718
## [6.1.0] - 2022-08-15
819
### Added
920
- Added dependency Injection

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ This API boilerplate includes the following:
3131
- In Base class in Operations, uncomment the line that establishes db connection
3232
- Update the login controller & user lib.
3333
- This project has a default editorconfig file. If needed customize it.
34+
- In program.cs
35+
1. Update CORS websites list
3436

3537
###### Remove the following
3638
- Values controller & values lib

src/WebApiBolierplate/API/API.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.2" />
17-
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
16+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.3" />
17+
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
1818
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
1919
</ItemGroup>
2020

src/WebApiBolierplate/API/Helpers/JWTHelper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public JWTHelper(IConfiguration configuration)
3434
/// /// <exception cref="ArgumentNullException">User Id is a must</exception>
3535
public string GenerateToken(string userId, string userRole = null, string userName = null, string companyId = null)
3636
{
37-
if (string.IsNullOrEmpty(userId))
38-
{
39-
throw new ArgumentNullException("userId", Errors.UserIdMandatory);
40-
}
37+
ArgumentException.ThrowIfNullOrEmpty(userId);
4138

4239
var token = new JwtTokenBuilder()
4340
.AddSecurityKey(_securityKey)

src/WebApiBolierplate/API/Helpers/JwtTokenBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public JwtSecurityToken Build()
4242
/// </summary>
4343
private void EnsureArguments()
4444
{
45-
if (securityKey == null)
46-
{
47-
throw new ArgumentNullException("Security Key");
48-
}
45+
ArgumentNullException.ThrowIfNull(securityKey);
4946

5047
if (expiryInDays == 0)
5148
{

src/WebApiBolierplate/API/Program.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66
using Microsoft.IdentityModel.Tokens;
77
using Microsoft.OpenApi.Models;
88
using System.Text;
9+
using Serilog;
10+
using Serilog.Events;
911

1012
var builder = WebApplication.CreateBuilder(args);
1113

12-
builder.Logging.AddFile("Logs/API.log");
14+
// If needed, Clear default providers
15+
builder.Logging.ClearProviders();
16+
17+
// Use Serilog
18+
builder.Host.UseSerilog((hostContext, services, loggerConfig) => {
19+
loggerConfig
20+
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
21+
.WriteTo.File( "Logs/api-.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true);
22+
});
1323

1424
// To prevent .NET and server info from being added to header if Kestrel is used
1525
builder.WebHost.ConfigureKestrel(serverOptions => {
@@ -96,7 +106,6 @@
96106
#endregion Configuring Services
97107

98108
var app = builder.Build();
99-
100109
if (app.Environment.IsDevelopment())
101110
{
102111
app.UseSwagger();
@@ -107,7 +116,17 @@
107116
app.UseHsts();
108117
}
109118

119+
app.UseCors(options =>
120+
options
121+
.AllowAnyHeader()
122+
.AllowAnyMethod()
123+
.WithOrigins(new[] { "https://localhost:7030/" })
124+
);
125+
110126
app.UseHttpsRedirection();
127+
128+
app.UseSerilogRequestLogging();
129+
111130
app.UseRouting();
112131
app.UseAuthentication();
113132
app.UseAuthorization();

src/WebApiBolierplate/API/web.config

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@
44
<!--
55
Used to configure IIS Server. Ignore if you are using Kestral.
66
For storing app data use appsettings.json
7-
-->
7+
Ref: https://learn.microsoft.com/en-us/iis/configuration/
8+
-->
89

910
<configuration>
10-
<system.webServer>
11-
<httpProtocol>
12-
<customHeaders>
13-
<remove name="X-Powered-By" />
14-
</customHeaders>
15-
</httpProtocol>
16-
<security>
17-
<requestFiltering removeServerHeader="true" />
18-
</security>
19-
</system.webServer>
11+
<system.webServer>
12+
<httpProtocol>
13+
<customHeaders>
14+
<!--
15+
Used to supress the X-Powered-By header in the response.
16+
Works in IIS version 8 and above.
17+
Ref: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/httpprotocol/customheaders/
18+
-->
19+
<remove name="X-Powered-By" />
20+
</customHeaders>
21+
</httpProtocol>
22+
<security>
23+
<!--
24+
Used to supress the IIS server header in the response.
25+
Works in IIS version 10 and above.
26+
Ref: https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/
27+
-->
28+
<requestFiltering removeServerHeader="true" />
29+
</security>
30+
</system.webServer>
2031
</configuration>

src/WebApiBolierplate/Core.Lib/Adapters/DBAdapter.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ public class DBAdapter
99
#region [Declarations]
1010

1111
private readonly SqlConnection connection;
12-
private const string SqlCommandNull = "SQL command cannot be null";
13-
private const string ConnectionStringNull = "Connection string cannot be empty";
14-
private const string SPNameNull = "Name of the stored procedure must be specified";
1512

1613
#endregion [Declarations]
1714

@@ -20,12 +17,10 @@ public class DBAdapter
2017
/// </summary>
2118
/// <param name="connectionString">The database connection string</param>
2219
/// <exception cref="ArgumentNullException"></exception>
20+
/// <exception cref="ArgumentException"></exception>
2321
public DBAdapter(string connectionString)
2422
{
25-
if (string.IsNullOrEmpty(connectionString))
26-
{
27-
throw new ArgumentNullException(connectionString, ConnectionStringNull);
28-
}
23+
ArgumentException.ThrowIfNullOrEmpty(connectionString);
2924

3025
connection = new SqlConnection(connectionString);
3126
connection.Open();
@@ -58,12 +53,10 @@ public DBAdapter(string connectionString)
5853
/// <param name="name">Name of the stored procedure</param>
5954
/// <returns></returns>
6055
/// <exception cref="ArgumentNullException"></exception>
56+
/// <exception cref="ArgumentException"></exception>
6157
public SqlCommand GetStoredProcedure(string name)
6258
{
63-
if (string.IsNullOrEmpty(name))
64-
{
65-
throw new ArgumentNullException(name, SPNameNull);
66-
}
59+
ArgumentException.ThrowIfNullOrEmpty(name);
6760
if (connection.State != ConnectionState.Open)
6861
{
6962
connection.Open();
@@ -84,10 +77,7 @@ public SqlCommand GetStoredProcedure(string name)
8477
/// <exception cref="ArgumentNullException"></exception>
8578
public object ExecuteScalar(SqlCommand dbCommand)
8679
{
87-
if (dbCommand == null)
88-
{
89-
throw new ArgumentNullException("dbCommand", SqlCommandNull);
90-
}
80+
ArgumentNullException.ThrowIfNull(dbCommand);
9181

9282
var result = dbCommand.ExecuteScalar();
9383
return result;
@@ -101,10 +91,7 @@ public object ExecuteScalar(SqlCommand dbCommand)
10191
/// <exception cref="ArgumentNullException"></exception>
10292
public IDataReader ExecuteReader(SqlCommand dbCommand)
10393
{
104-
if (dbCommand == null)
105-
{
106-
throw new ArgumentNullException("dbCommand", SqlCommandNull);
107-
}
94+
ArgumentNullException.ThrowIfNull(dbCommand);
10895

10996
var result = dbCommand.ExecuteReader();
11097
return result;
@@ -118,10 +105,7 @@ public IDataReader ExecuteReader(SqlCommand dbCommand)
118105
/// <exception cref="ArgumentNullException"></exception>
119106
public int ExecuteNonQuery(SqlCommand dbCommand)
120107
{
121-
if (dbCommand == null)
122-
{
123-
throw new ArgumentNullException("dbCommand", SqlCommandNull);
124-
}
108+
ArgumentNullException.ThrowIfNull(dbCommand);
125109

126110
var result = dbCommand.ExecuteNonQuery();
127111
return result;

src/WebApiBolierplate/Core.Lib/Security/EncryptionHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public EncryptionHelper()
1919
private Aes BuildAesEncryptor(string encryptionKey)
2020
{
2121
var aesEncryptor = Aes.Create();
22-
var pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
22+
var pdb = new Rfc2898DeriveBytes(
23+
password: encryptionKey,
24+
salt: "335f0298-9eae-4285-890e-ef7243c974f0"u8.ToArray(),
25+
iterations: 5033,
26+
hashAlgorithm: HashAlgorithmName.SHA512);
2327
aesEncryptor.Key = pdb.GetBytes(32);
2428
aesEncryptor.IV = pdb.GetBytes(16);
2529
return aesEncryptor;

src/WebApiBolierplate/Core.Lib/Security/HashHelper.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ public HashHelper()
1717
/// <param name="plainText"></param>
1818
/// <returns></returns>
1919
/// <exception cref="ArgumentNullException"></exception>
20+
/// <exception cref="ArgumentException"></exception>
2021
public string HashBCrypt(string plainText)
2122
{
22-
if (string.IsNullOrEmpty(plainText))
23-
{
24-
throw new ArgumentNullException("plainText", AllPrametersMandatory);
25-
}
23+
ArgumentException.ThrowIfNullOrEmpty(plainText);
2624

2725
var hash = BCrypt.Net.BCrypt.HashPassword(plainText, workFactor: 10);
2826
return hash;
@@ -35,12 +33,11 @@ public string HashBCrypt(string plainText)
3533
/// <param name="hash"></param>
3634
/// <returns></returns>
3735
/// <exception cref="ArgumentNullException"></exception>
36+
/// <exception cref="ArgumentException"></exception>
3837
public bool VerifyBCrypt(string plainText, string hash)
3938
{
40-
if (string.IsNullOrEmpty(plainText) || string.IsNullOrEmpty(hash))
41-
{
42-
throw new ArgumentNullException("plainText", AllPrametersMandatory);
43-
}
39+
ArgumentException.ThrowIfNullOrEmpty(plainText);
40+
ArgumentException.ThrowIfNullOrEmpty(hash);
4441

4542
var isMatch = BCrypt.Net.BCrypt.Verify(plainText, hash);
4643
return isMatch;

0 commit comments

Comments
 (0)