2025-09-27 10:36:00 +03:30
|
|
|
using System.Diagnostics;
|
|
|
|
|
using FrontOffice.BFF.Application.Common.Interfaces;
|
|
|
|
|
using FrontOffice.BFF.Infrastructure.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2025-11-14 08:57:16 +03:30
|
|
|
using Microsoft.IdentityModel.Logging;
|
2025-09-28 00:45:26 +03:30
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2025-09-27 10:36:00 +03:30
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
public static class ConfigureServices
|
|
|
|
|
{
|
2025-11-14 08:57:16 +03:30
|
|
|
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services,
|
|
|
|
|
IConfiguration configuration)
|
2025-09-27 10:36:00 +03:30
|
|
|
{
|
|
|
|
|
services.AddSingleton<IApplicationContractContext, ApplicationContractContext>();
|
2025-09-28 00:45:26 +03:30
|
|
|
services.AddSingleton<IKavenegarService, KavenegarService>();
|
2025-09-27 10:36:00 +03:30
|
|
|
services.AddInfrastructureGrpcServices(configuration);
|
2025-11-14 08:57:16 +03:30
|
|
|
|
2025-09-27 10:36:00 +03:30
|
|
|
#region AddAuthentication
|
|
|
|
|
|
2025-11-14 08:57:16 +03:30
|
|
|
#if DEBUG
|
|
|
|
|
IdentityModelEventSource.ShowPII = true;
|
|
|
|
|
#endif
|
2025-09-27 10:36:00 +03:30
|
|
|
var message = "";
|
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(jwtBearerOptions =>
|
|
|
|
|
{
|
2025-09-28 00:45:26 +03:30
|
|
|
//jwtBearerOptions.Authority = configuration["Authentication:Authority"];
|
|
|
|
|
//jwtBearerOptions.Audience = configuration["Authentication:Audience"];
|
|
|
|
|
//jwtBearerOptions.TokenValidationParameters.ValidateAudience = false;
|
|
|
|
|
//jwtBearerOptions.TokenValidationParameters.ValidateIssuer = true;
|
|
|
|
|
//jwtBearerOptions.TokenValidationParameters.ValidateIssuerSigningKey = false;
|
|
|
|
|
|
|
|
|
|
jwtBearerOptions.SaveToken = true;
|
|
|
|
|
jwtBearerOptions.RequireHttpsMetadata = false;
|
|
|
|
|
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
2025-11-14 08:57:16 +03:30
|
|
|
ValidateIssuer = false, //todo change to true in production
|
|
|
|
|
ValidateAudience = false, //todo change to true in production
|
2025-09-28 00:45:26 +03:30
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
ValidIssuer = configuration["JwtIssuer"],
|
|
|
|
|
ValidAudience = configuration["JwtAudience"],
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtSecurityKey"]))
|
|
|
|
|
};
|
2025-09-27 10:36:00 +03:30
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
jwtBearerOptions.Events = new JwtBearerEvents
|
|
|
|
|
{
|
|
|
|
|
OnAuthenticationFailed = ctx =>
|
|
|
|
|
{
|
|
|
|
|
ctx.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
|
|
|
|
message += "From OnAuthenticationFailed:\n";
|
|
|
|
|
message += ctx.Exception.Message;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
OnChallenge = ctx =>
|
|
|
|
|
{
|
|
|
|
|
message += "From OnChallenge:\n";
|
|
|
|
|
ctx.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
|
|
|
|
ctx.Response.ContentType = "text/plain";
|
|
|
|
|
return ctx.Response.WriteAsync(message);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
OnMessageReceived = ctx =>
|
|
|
|
|
{
|
|
|
|
|
message = "From OnMessageReceived:\n";
|
|
|
|
|
ctx.Request.Headers.TryGetValue("Authorization", out var BearerToken);
|
|
|
|
|
if (BearerToken.Count == 0)
|
|
|
|
|
BearerToken = "no Bearer token sent\n";
|
|
|
|
|
message += "Authorization Header sent: " + BearerToken + "\n";
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
OnTokenValidated = ctx =>
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("token: " + ctx.SecurityToken.ToString());
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|