forked from MDCPS/DamageAssessment_Backend
User Role and Authorization features added
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using DamageAssesment.Api.Surveys.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@ -25,6 +26,7 @@ namespace DamageAssesment.Api.Surveys.Controllers
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{Id}")]
|
||||
public async Task<ActionResult> GetSurveysAsync(int Id)
|
||||
{
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.21" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
|
@ -1,11 +1,32 @@
|
||||
using DamageAssesment.Api.Surveys.Db;
|
||||
using DamageAssesment.Api.Surveys.Interfaces;
|
||||
using DamageAssesment.Api.Surveys.Providers;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey");
|
||||
builder.Services.AddAuthentication(item =>
|
||||
{
|
||||
item.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
item.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(item =>
|
||||
{
|
||||
item.RequireHttpsMetadata = true;
|
||||
item.SaveToken = true;
|
||||
item.TokenValidationParameters = new TokenValidationParameters()
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authkey)),
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
@ -26,6 +47,7 @@ if (app.Environment.IsDevelopment())
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
@ -26,7 +26,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
surveyDbContext.Surveys.Add(new Db.Survey { Id = 1, Title = "Sample Survey Title:Damage Assesment 2014", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
||||
surveyDbContext.Surveys.Add(new Db.Survey { Id = 2, Title = "Sample Survey Title: Damage Assesment 2016", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
||||
surveyDbContext.Surveys.Add(new Db.Survey { Id = 3, Title = "Sample Survey Title: Damage Assesment 2018", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
||||
surveyDbContext.SaveChanges();
|
||||
surveyDbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
var surveys = await surveyDbContext.Surveys.ToListAsync();
|
||||
survey.Id = surveys.Count + 1;
|
||||
surveyDbContext.Surveys.Add(mapper.Map<Models.Survey, Db.Survey>(survey));
|
||||
surveyDbContext.SaveChanges();
|
||||
await surveyDbContext.SaveChangesAsync();
|
||||
return (true, survey, "Successful");
|
||||
}
|
||||
else
|
||||
@ -110,7 +110,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
_survey.IsEnabled = survey.IsEnabled;
|
||||
_survey.StartDate = survey.StartDate;
|
||||
_survey.EndDate = survey.EndDate;
|
||||
surveyDbContext.SaveChanges();
|
||||
await surveyDbContext.SaveChangesAsync();
|
||||
return (true, mapper.Map<Db.Survey, Models.Survey>(_survey), "Successful");
|
||||
}
|
||||
else
|
||||
@ -141,7 +141,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
if (survey != null)
|
||||
{
|
||||
surveyDbContext.Surveys.Remove(survey);
|
||||
surveyDbContext.SaveChanges();
|
||||
await surveyDbContext.SaveChangesAsync();
|
||||
return (true, mapper.Map<Db.Survey, Models.Survey>(survey), $"Survey Id: {Id} deleted Successfuly");
|
||||
}
|
||||
else
|
||||
|
@ -1,4 +1,7 @@
|
||||
{
|
||||
"JwtSettings": {
|
||||
"securitykey": "bWlhbWkgZGFkZSBzY2hvb2xzIHNlY3JldCBrZXk="
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
Reference in New Issue
Block a user