Skip to content

Commit dd3ef4a

Browse files
committed
Merge branch 'html-ui' into dev
2 parents 311a38c + 70e95e3 commit dd3ef4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3624
-579
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.IdentityModel.Tokens;
2+
using System;
3+
using System.Text;
4+
5+
namespace SampleWebApp.Authentication.Jwt
6+
{
7+
public class JwtKeyGenerator
8+
{
9+
public static SymmetricSecurityKey Generate(string secret)
10+
{
11+
if (string.IsNullOrEmpty(secret))
12+
throw new ArgumentNullException(nameof(secret));
13+
14+
return new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secret));
15+
}
16+
}
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.IdentityModel.Tokens;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IdentityModel.Tokens.Jwt;
7+
using System.Security.Claims;
8+
9+
namespace SampleWebApp.Authentication.Jwt
10+
{
11+
public class JwtTokenGenerator
12+
{
13+
private readonly IConfiguration _configuration;
14+
15+
public JwtTokenGenerator(IConfiguration configuration)
16+
{
17+
_configuration = configuration;
18+
}
19+
20+
public string GenerateJwtToken(IdentityUser user)
21+
{
22+
var claims = new List<Claim>
23+
{
24+
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
25+
new(ClaimTypes.Name, user.UserName),
26+
new(ClaimTypes.NameIdentifier, user.UserName),
27+
new(ClaimTypes.UserData, user.Id)
28+
};
29+
30+
var key = JwtKeyGenerator.Generate(_configuration["Jwt:SecretKey"]);
31+
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
32+
var expires = DateTime.Now.AddDays(Convert.ToDouble(_configuration["Jwt:ExpireDays"]));
33+
34+
var token = new JwtSecurityToken(
35+
_configuration["Jwt:Issuer"],
36+
_configuration["Jwt:Audience"],
37+
claims,
38+
expires: expires,
39+
signingCredentials: credentials
40+
);
41+
42+
return new JwtSecurityTokenHandler().WriteToken(token);
43+
}
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.AspNetCore.Mvc;
3+
using SampleWebApp.Authentication.Jwt;
4+
using System.Threading.Tasks;
5+
6+
namespace SampleWebApp.Controllers
7+
{
8+
[ApiController, Route("api/[controller]")]
9+
public class UserController : ControllerBase
10+
{
11+
private readonly JwtTokenGenerator _jwtTokenGenerator;
12+
private readonly UserManager<IdentityUser> _userManager;
13+
14+
public UserController(JwtTokenGenerator jwtTokenGenerator, UserManager<IdentityUser> userManager)
15+
{
16+
_jwtTokenGenerator = jwtTokenGenerator;
17+
_userManager = userManager;
18+
}
19+
20+
[HttpGet]
21+
public async Task<ActionResult<string>> Login(string email)
22+
{
23+
var user = await _userManager.FindByEmailAsync(email);
24+
if (user == null)
25+
return Ok();
26+
27+
var token = _jwtTokenGenerator.GenerateJwtToken(user);
28+
29+
return Ok(token);
30+
}
31+
}
32+
}

samples/SampleWebApp/SampleWebApp.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.1" />
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" />
1011
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.1" />
1112
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.1" />
1213
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
@@ -17,10 +18,11 @@
1718
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
1819
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
1920
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.6.0" />
21+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
2022
</ItemGroup>
2123

2224
<ItemGroup>
23-
<ProjectReference Include="..\..\src\Serilog.Ui.MongoDbServerProvider\Serilog.Ui.MongoDbServerProvider.csproj" />
25+
<ProjectReference Include="..\..\src\Serilog.Ui.MongoDbProvider\Serilog.Ui.MongoDbProvider.csproj" />
2426
<ProjectReference Include="..\..\src\Serilog.Ui.MsSqlServerProvider\Serilog.Ui.MsSqlServerProvider.csproj" />
2527
<ProjectReference Include="..\..\src\Serilog.Ui.PostgreSqlProvider\Serilog.Ui.PostgreSqlProvider.csproj" />
2628
<ProjectReference Include="..\..\src\Serilog.Ui.Web\Serilog.Ui.Web.csproj" />

samples/SampleWebApp/Startup.cs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Identity;
45
using Microsoft.EntityFrameworkCore;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Hosting;
9+
using Microsoft.IdentityModel.Tokens;
10+
using SampleWebApp.Authentication.Jwt;
811
using SampleWebApp.Data;
912
using Serilog.Ui.MsSqlServerProvider;
1013
using Serilog.Ui.Web;
@@ -28,12 +31,22 @@ public void ConfigureServices(IServiceCollection services)
2831

2932
services.AddDatabaseDeveloperPageExceptionFilter();
3033

31-
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
32-
.AddEntityFrameworkStores<ApplicationDbContext>();
34+
AddAuthentication(services);
35+
36+
services.AddScoped<JwtTokenGenerator>();
37+
38+
services.AddControllersWithViews();
39+
services.AddRazorPages();
3340

34-
var mvcBuilder = services.AddControllersWithViews();
41+
services.AddSerilogUi(options => options
42+
.EnableAuthorization(authOption =>
43+
{
44+
authOption.AuthenticationType = AuthenticationType.Jwt;
45+
authOption.Usernames = new[] { "[email protected]" };
46+
})
47+
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), "Logs"));
3548

36-
services.AddSerilogUi(mvcBuilder, options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), "Logs"));
49+
services.AddSwaggerGen();
3750
}
3851

3952
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -53,10 +66,20 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5366
app.UseHttpsRedirection();
5467
app.UseStaticFiles();
5568

69+
app.UseSwagger();
70+
71+
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
72+
// specifying the Swagger JSON endpoint.
73+
app.UseSwaggerUI(c =>
74+
{
75+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
76+
});
77+
5678
app.UseRouting();
5779

5880
app.UseAuthentication();
5981
app.UseAuthorization();
82+
app.UseSerilogUi();
6083

6184
app.UseEndpoints(endpoints =>
6285
{
@@ -66,5 +89,33 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6689
endpoints.MapRazorPages();
6790
});
6891
}
92+
93+
private void AddAuthentication(IServiceCollection services)
94+
{
95+
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
96+
.AddEntityFrameworkStores<ApplicationDbContext>();
97+
98+
services
99+
.AddAuthentication(options =>
100+
{
101+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
102+
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
103+
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
104+
})
105+
.AddJwtBearer(options =>
106+
{
107+
options.TokenValidationParameters =
108+
new TokenValidationParameters
109+
{
110+
ValidateIssuer = false,
111+
ValidateAudience = true,
112+
ValidateLifetime = true,
113+
ValidateIssuerSigningKey = true,
114+
ValidIssuer = Configuration["Jwt:Issuer"],
115+
ValidAudience = Configuration["Jwt:Audience"],
116+
IssuerSigningKey = JwtKeyGenerator.Generate(Configuration["Jwt:SecretKey"])
117+
};
118+
});
119+
}
69120
}
70121
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft": "Warning",
6-
"Microsoft.Hosting.Lifetime": "Information"
7-
}
8-
}
9-
}
2+
}

samples/SampleWebApp/appsettings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55

66
"AllowedHosts": "*",
77

8+
"Jwt": {
9+
"Audience": "SampleClient",
10+
"Issuer": "http://localhost:5000",
11+
"SecretKey": "Th!$P@ssw0rd",
12+
"ExpireDays": "30"
13+
},
14+
815
"Serilog": {
916
"Using": [ "Serilog.Sinks.MSSqlServer" ],
10-
"MinimumLevel": "Information",
17+
"MinimumLevel": "Warning",
1118
"WriteTo": [
1219
{
1320
"Name": "MSSqlServer",

src/Serilog.Ui.MsSqlServerProvider/Serilog.Ui.MsSqlServerProvider.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
8+
<PlatformTarget>x64</PlatformTarget>
9+
<WarningLevel>0</WarningLevel>
10+
</PropertyGroup>
11+
712
<ItemGroup>
813
<ProjectReference Include="..\Serilog.Ui.Core\Serilog.Ui.Core.csproj" />
914
</ItemGroup>

src/Serilog.Ui.MsSqlServerProvider/SqlServerDataProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, stri
6363
var logs = await connection.QueryAsync<SqlServerLogModel>(queryBuilder.ToString(),
6464
new
6565
{
66-
Offset = page,
66+
Offset = page * count,
6767
Count = count,
6868
Level = level,
6969
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null

src/Serilog.Ui.Web/Controllers/LogsController.cs

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)