- Added user roles to JWT token generation.
- Assigned default role ("User") during user registration.
- Implemented role-based access control for API endpoints.
- Updated error messages for better clarity.
- Improved AnalyticsService for more accurate survey insights.
Task completed. Awaiting review.
91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using survey_beta.DataBaseContext;
|
|
using survey_beta.Mappers.Profiles;
|
|
using survey_beta.Models;
|
|
using System.Text;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|
{
|
|
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
|
Description = "Please enter JWT with Bearer into field",
|
|
Name = "Authorization",
|
|
Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey
|
|
});
|
|
|
|
c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
|
{
|
|
Reference = new Microsoft.OpenApi.Models.OpenApiReference
|
|
{
|
|
Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
new string[] {}
|
|
}
|
|
});
|
|
});
|
|
|
|
builder.Services.AddIdentity<User, IdentityRole>()
|
|
.AddEntityFrameworkStores<AppDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
|
|
};
|
|
});
|
|
|
|
builder.Services.AddScoped<UsersServices>();
|
|
builder.Services.AddScoped<SurveyService>();
|
|
builder.Services.AddScoped<ResponsesService>();
|
|
builder.Services.AddScoped<AnalyticsServices>();
|
|
builder.Services.AddAutoMapper(typeof(MappingProfile));
|
|
builder.Services.AddAutoMapper(typeof(UserProfile));
|
|
builder.Services.AddAutoMapper(typeof(ResponseProfile));
|
|
builder.Services.AddAutoMapper(typeof(AnalyticsProfile));
|
|
builder.Services.AddAutoMapper(typeof(AnswerProfile));
|
|
builder.Services.AddAutoMapper(typeof(ChoiceProfile));
|
|
builder.Services.AddAutoMapper(typeof(QuestionProfile));
|
|
builder.Services.AddAutoMapper(typeof(SurveyProfile));
|
|
var app = builder.Build();
|
|
|
|
// HTTP request pipeline
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.MapControllers();
|
|
app.Run();
|