-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
49 lines (39 loc) · 2.01 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using BioID.Services;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
var builder = WebApplication.CreateBuilder(args);
// Add gRPC bws client
builder.Services.AddGrpcClient<BioIDWebService.BioIDWebServiceClient>(o =>
{
// Configure grpc server endpoint from appsettings.json
var gprcEndpoint = builder.Configuration.GetSection("BwsGrpcApiSettings")["Endpoint"] ?? throw new InvalidOperationException("The gRPC endpoint is not specified or is incorrect.");
o.Address = new Uri(gprcEndpoint);
})
.AddCallCredentials((context, metadata, serviceProvider) =>
{
// Generate JWT token
var key = builder.Configuration.GetSection("BwsGrpcApiSettings")["AccessKey"] ?? throw new InvalidOperationException("The grpc access key could not be found.");
var securityKey = new SymmetricSecurityKey(Convert.FromBase64String(key));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha512);
var clientID = builder.Configuration.GetSection("BwsGrpcApiSettings")["ClientId"] ?? throw new InvalidOperationException("The grpc clientId could not be found.");
List<Claim> claims = [new Claim(JwtRegisteredClaimNames.Sub, clientID)];
var now = DateTime.UtcNow;
string token = new JwtSecurityTokenHandler().CreateEncodedJwt("demoWeApp", "bws", new ClaimsIdentity(claims), now, now.AddMinutes(10), now, credentials);
metadata.Add("Authorization", $"Bearer {token}");
return Task.CompletedTask;
});
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.Run();