forked from MDCPS/DamageAssessment_Backend
		
	merged azure and user access changes to docker azure branch
This commit is contained in:
		| @ -0,0 +1,117 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     public class UsersAccessController : ControllerBase | ||||
|     { | ||||
|         private IUsersAccessProvider userAccessProvider; | ||||
|  | ||||
|         public UsersAccessController(IUsersAccessProvider userAccessProvider) | ||||
|         { | ||||
|             this.userAccessProvider = userAccessProvider; | ||||
|         } | ||||
|         [Authorize(Policy = "Dadeschools")] | ||||
|         [HttpPost("token/{employecode}")] | ||||
|         public async Task<ActionResult> AuthenticateAsync(string employecode) | ||||
|         { | ||||
|               var result = await userAccessProvider.AuthenticateAsync(employecode); | ||||
|               if (result.IsSuccess) | ||||
|               { | ||||
|                   return Ok(result.TokenResponse); | ||||
|               } | ||||
|               return Unauthorized(result.ErrorMessage); | ||||
|         } | ||||
|  | ||||
|         [Authorize(Policy = "Dadeschools")] | ||||
|         [HttpPost("refreshtoken")] | ||||
|         public async Task<ActionResult> RefreshTokenAsync(TokenResponse tokenResponse) | ||||
|         { | ||||
|             var result = await userAccessProvider.RefreshTokenAsync(tokenResponse); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.TokenResponse); | ||||
|             } | ||||
|             return Unauthorized(result.ErrorMessage); | ||||
|         } | ||||
|  | ||||
|         [Authorize(Policy = "DamageApp", Roles ="admin")] | ||||
|         [HttpGet("users")] | ||||
|         public async Task<ActionResult> GetUsersAsync() | ||||
|         { | ||||
|             var result = await userAccessProvider.GetUsersAsync(); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.Users); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|  | ||||
|         [Authorize(Policy = "DamageApp", Roles = "admin")] | ||||
|         [HttpGet("users/{Id}")] | ||||
|         public async Task<ActionResult> GetUsersAsync(int Id) | ||||
|         { | ||||
|             var result = await userAccessProvider.GetUsersAsync(Id); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.User); | ||||
|             } | ||||
|             return NotFound(); | ||||
|         } | ||||
|  | ||||
|         [Authorize(Policy = "DamageApp", Roles = "admin")] | ||||
|         [HttpGet("roles")] | ||||
|         public async Task<ActionResult> GetRolesAsync() | ||||
|         { | ||||
|             var result = await userAccessProvider.GetRolesAsync(); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.Roles); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|         [Authorize(Policy = "DamageApp", Roles = "admin")] | ||||
|         [HttpPost("users")] | ||||
|         public async Task<ActionResult> PostUserAsync(User user) | ||||
|         { | ||||
|             var result = await userAccessProvider.PostUserAsync(user); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.User); | ||||
|             } | ||||
|             return BadRequest(result.ErrorMessage); | ||||
|         } | ||||
|  | ||||
|         [Authorize(Policy = "DamageApp", Roles = "admin")] | ||||
|         [HttpPut("users/{Id}")] | ||||
|         public async Task<ActionResult> PutUserAsync(int Id, User user) | ||||
|         { | ||||
|             var result = await userAccessProvider.PutUserAsync(Id, user); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.User); | ||||
|             } | ||||
|             if (result.ErrorMessage == "Not Found") | ||||
|                 return NotFound(result.ErrorMessage); | ||||
|  | ||||
|             return BadRequest(result.ErrorMessage); | ||||
|         } | ||||
|  | ||||
|         [Authorize(Policy = "DamageApp", Roles = "admin")] | ||||
|         [HttpDelete("users/{Id}")] | ||||
|         public async Task<ActionResult> DeleteUserAsync(int Id) | ||||
|         { | ||||
|             var result = await userAccessProvider.DeleteUserAsync(Id); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.User); | ||||
|             } | ||||
|             return NotFound(); | ||||
|         } | ||||
|  | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,29 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <TargetFramework>net6.0</TargetFramework> | ||||
|     <Nullable>enable</Nullable> | ||||
|     <ImplicitUsings>enable</ImplicitUsings> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /> | ||||
|     <PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.21" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9"> | ||||
|       <PrivateAssets>all</PrivateAssets> | ||||
|       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||||
|     </PackageReference> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9"> | ||||
|       <PrivateAssets>all</PrivateAssets> | ||||
|       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||||
|     </PackageReference> | ||||
|     <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.10" /> | ||||
|     <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||||
|     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
| @ -0,0 +1,21 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
| using System.Text.Json.Serialization; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Db | ||||
| { | ||||
|     public class Role | ||||
|     { | ||||
|         [Key] | ||||
|         public int Id { get; set; } | ||||
|  | ||||
|         [StringLength(100)] | ||||
|         [Required] | ||||
|         public string Name { get; set; } | ||||
|  | ||||
|         // add a status field | ||||
|  | ||||
|         [StringLength(100)] | ||||
|         public string? Description { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,17 @@ | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Db | ||||
| { | ||||
|     public  class Token | ||||
|     { | ||||
|         [Key] | ||||
|         public int Id { get; set; } | ||||
|         [Required] | ||||
|         [ForeignKey("User")] | ||||
|         public int UserId { get; set; } | ||||
|         public string? RefreshToken { get; set; } | ||||
|         public bool? IsActive { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,31 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
| using System.Text.Json.Serialization; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Db | ||||
| { | ||||
|     public class User | ||||
|     { | ||||
|         [Key] | ||||
|         public int Id { get; set; } | ||||
|  | ||||
|         [ForeignKey("Employee")] | ||||
|         public int EmployeeId { get; set; } | ||||
|  | ||||
|         [Required] | ||||
|         [StringLength(50)] | ||||
|         public string EmployeeCode { get; set; } | ||||
|  | ||||
|         [ForeignKey("Role")] | ||||
|         [Required] | ||||
|         public int RoleId { get; set; } | ||||
|         [Required] | ||||
|         public bool IsActive { get; set; } = true; | ||||
|  | ||||
|         [Required] | ||||
|         public DateTime CreateDate { get; set; } = DateTime.Now; | ||||
|  | ||||
|         public DateTime? UpdateDate { get; set; } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,39 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.Extensions.Configuration; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Db | ||||
| { | ||||
|     public class UsersAccessDbContext : DbContext | ||||
|     { | ||||
|         public DbSet<Db.User> Users { get; set; } | ||||
|         public DbSet<Db.Role> Roles { get; set; } | ||||
|         public DbSet<Db.Token> Tokens { get; set; } | ||||
|         private IConfiguration _Configuration { get; set; } | ||||
|         public UsersAccessDbContext(DbContextOptions options, IConfiguration configuration) : base(options) | ||||
|         { | ||||
|             _Configuration = configuration; | ||||
|         } | ||||
|         protected override void OnConfiguring(DbContextOptionsBuilder options) | ||||
|         { | ||||
|             // connect to sql server with connection string from app settings | ||||
|             options.UseSqlServer(_Configuration.GetConnectionString("UsersAccessConnection")); | ||||
|         } | ||||
|  | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             base.OnModelCreating(modelBuilder); | ||||
|  | ||||
|             modelBuilder.Entity<User>() | ||||
|                 .Property(item => item.Id) | ||||
|                 .ValueGeneratedOnAdd(); | ||||
|  | ||||
|             modelBuilder.Entity<Role>() | ||||
|             .Property(item => item.Id) | ||||
|             .ValueGeneratedOnAdd(); | ||||
|  | ||||
|             modelBuilder.Entity<Token>() | ||||
|             .Property(item => item.Id) | ||||
|             .ValueGeneratedOnAdd(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,10 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Interfaces | ||||
| { | ||||
|     public interface IEmployeeServiceProvider | ||||
|     { | ||||
|         Task<List<Employee>> getEmployeesAsync(); | ||||
|         Task<Employee> getEmployeeAsync(int employeeId); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,12 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Interfaces | ||||
| { | ||||
|     public interface IRoleProvider | ||||
|     { | ||||
|        Task<(bool IsSuccess, IEnumerable< Models.Role> Roles, string ErrorMessage)> GetRolesAsync(); | ||||
|        Task<(bool IsSuccess, Models.Role Roles, string ErrorMessage)> GetRolesAsync(int Id); | ||||
|        Task<(bool IsSuccess, Models.Role Role, string ErrorMessage)> PostRoleAsync(Models.Role Role); | ||||
|        Task<(bool IsSuccess, Models.Role Role, string ErrorMessage)> PutRoleAsync(int Id,Models.Role Role); | ||||
|        Task<(bool IsSuccess, Models.Role Role, string ErrorMessage)> DeleteRoleAsync(int Id); | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
| using System.Security.Claims; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Interfaces | ||||
| { | ||||
|     public interface ITokenServiceProvider | ||||
|     { | ||||
|         Task<string> GenerateToken(Models.User user); | ||||
|         Task<TokenResponse> TokenAuthenticate(Models.User user, Claim[] claims); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,17 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Interfaces | ||||
| { | ||||
|     public interface IUsersAccessProvider | ||||
|     { | ||||
|        public Task<(bool IsSuccess, IEnumerable< Models.User> Users, string ErrorMessage)> GetUsersAsync(); | ||||
|         public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> GetUsersAsync(int Id); | ||||
|         public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> PostUserAsync(Models.User User); | ||||
|         public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> PutUserAsync(int Id,Models.User User); | ||||
|         public Task<(bool IsSuccess, Models.User User, string ErrorMessage)> DeleteUserAsync(int Id); | ||||
|         public Task<(bool IsSuccess, IEnumerable<Models.Role> Roles, string ErrorMessage)> GetRolesAsync(); | ||||
|         public  Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)> AuthenticateAsync(string employeCode); | ||||
|         public Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)>RefreshTokenAsync(TokenResponse tokenResponse); | ||||
|         public void seedData(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,7 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Interfaces | ||||
| { | ||||
|     public interface IHttpUtil | ||||
|     { | ||||
|         Task<string> SendAsync(HttpMethod method, string url, string JsonInput); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Models | ||||
| { | ||||
|     public class Employee | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public string EmployeeCode { get; set; } | ||||
|         public string Name { get; set; } | ||||
|         public DateTime BirthDate { get; set; } | ||||
|         public string OfficePhoneNumber { get; set; } | ||||
|         public string Email { get; set; } | ||||
|         public bool IsActive { get; set; } | ||||
|         public string PreferredLanguage { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,9 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| namespace DamageAssesment.Api.UsersAccess.Models | ||||
| { | ||||
|  | ||||
|     public class JwtSettings | ||||
|     { | ||||
|         public string securitykey { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,8 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Models | ||||
| { | ||||
|     public class Role {  | ||||
|         public int Id { get; set; } | ||||
|         public string Name { get; set; } | ||||
|         public string Description { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,10 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Models | ||||
| { | ||||
|     public  class Token | ||||
|     {  | ||||
|         public string Id { get; set; } | ||||
|         public int UserId { get; set; }  | ||||
|         public string RefreshToken { get; set; } | ||||
|         public bool IsActive { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,8 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Models | ||||
| { | ||||
|     public class TokenResponse | ||||
|     { | ||||
|         public string? jwttoken { get; set; } | ||||
|         public string? refreshtoken { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Models | ||||
| { | ||||
|     public class User | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public int EmployeeId { get; set; } | ||||
|         public string EmployeeCode { get; set; } | ||||
|         public int RoleId { get; set; } | ||||
|         public bool IsActive { get; set; } | ||||
|         public DateTime CreateDate { get; set; } | ||||
|         public DateTime UpdateDate { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,5 @@ | ||||
| public class UserCredentials | ||||
| { | ||||
|     public string username { get; set; } | ||||
|    // public string? password { get; set; } | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| namespace DamageAssesment.Api.UsersAccess.Profiles | ||||
| { | ||||
|     public class UsersAccessProfile : AutoMapper.Profile | ||||
|     { | ||||
|         public UsersAccessProfile() | ||||
|         { | ||||
|             CreateMap<Db.User, Models.User>(); | ||||
|             CreateMap<Models.User, Db.User>(); | ||||
|  | ||||
|             CreateMap<Db.Role, Models.Role>(); | ||||
|             CreateMap<Models.Role, Db.Role>(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										146
									
								
								DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Program.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,146 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Db; | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
| using DamageAssesment.Api.UsersAccess.Providers; | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
| using System.Text; | ||||
| using Polly; | ||||
| using DamageAssesment.Api.UsersAccess.Services; | ||||
| using Microsoft.OpenApi.Models; | ||||
| using System.Reflection; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
|  | ||||
| const int maxApiCallRetries = 3; | ||||
| const int intervalToRetry = 2; //2 seconds | ||||
| const int maxRetryForCircuitBraker = 5; | ||||
| const int intervalForCircuitBraker = 5; //5 seconds | ||||
|  | ||||
| var builder = WebApplication.CreateBuilder(args); | ||||
|  | ||||
| // Add services to the container. | ||||
| var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey"); | ||||
|  | ||||
|  | ||||
| builder.Services.AddAuthentication(). | ||||
|     AddJwtBearer("DamageApp", 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 | ||||
|     }; | ||||
| }).AddJwtBearer("Dadeschools", options => | ||||
| { | ||||
|     options.Authority = builder.Configuration["Dadeschools:Authority"]; | ||||
|     options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" }; | ||||
|     options.TokenValidationParameters.ValidateAudience = false; | ||||
| }); | ||||
|  | ||||
|  | ||||
| builder.Services.AddAuthorization(options => | ||||
| { | ||||
|     var DamageAppPolicy = new AuthorizationPolicyBuilder() | ||||
|         .RequireAuthenticatedUser() | ||||
|         .AddAuthenticationSchemes("DamageApp") | ||||
|         .Build(); | ||||
|     var DadeschoolsPolicy = new AuthorizationPolicyBuilder() | ||||
|         .RequireAuthenticatedUser() | ||||
|         .AddAuthenticationSchemes("Dadeschools") | ||||
|         .Build(); | ||||
|     var allPolicy = new AuthorizationPolicyBuilder() | ||||
|         .RequireAuthenticatedUser() | ||||
|         .AddAuthenticationSchemes("DamageApp", "Dadeschools") | ||||
|         .Build(); | ||||
|     options.AddPolicy("DamageApp", DamageAppPolicy); | ||||
|     options.AddPolicy("Dadeschools", DadeschoolsPolicy); | ||||
|     options.AddPolicy("AllPolicies", allPolicy); | ||||
|     options.DefaultPolicy = options.GetPolicy("DamageApp")!; | ||||
| }); | ||||
|  | ||||
| var _jwtsettings = builder.Configuration.GetSection("JwtSettings"); | ||||
| builder.Services.Configure<JwtSettings>(_jwtsettings); | ||||
|  | ||||
| builder.Services.AddControllers(); | ||||
| // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||||
| builder.Services.AddScoped<IUsersAccessProvider, UsersAccessProvider>(); | ||||
| builder.Services.AddScoped<ITokenServiceProvider, TokenServiceProvider>(); | ||||
| builder.Services.AddScoped<IEmployeeServiceProvider, EmployeeServiceProvider>(); | ||||
|  | ||||
| builder.Services.AddHttpClient<IHttpUtil, HttpUtil>(). | ||||
|     AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))). | ||||
|     AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker))); | ||||
|  | ||||
| builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); | ||||
| builder.Services.AddEndpointsApiExplorer(); | ||||
| //builder.Services.AddSwaggerGen(); | ||||
|  | ||||
| builder.Services.AddSwaggerGen(options => | ||||
| { | ||||
|  | ||||
|     // Include XML comments from your assembly | ||||
|     var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||||
|     var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); | ||||
|     //options.IncludeXmlComments(xmlPath); | ||||
|  | ||||
|     OpenApiSecurityScheme securityDefinition = new OpenApiSecurityScheme() | ||||
|     { | ||||
|         Name = "Bearer", | ||||
|         BearerFormat = "JWT", | ||||
|         Scheme = "bearer", | ||||
|         Description = "Specify the authorization token.", | ||||
|         In = ParameterLocation.Header, | ||||
|         Type = SecuritySchemeType.Http, | ||||
|     }; | ||||
|  | ||||
|     options.AddSecurityDefinition("jwt_auth", securityDefinition); | ||||
|  | ||||
|     // Make sure swagger UI requires a Bearer token specified | ||||
|     OpenApiSecurityScheme securityScheme = new OpenApiSecurityScheme() | ||||
|     { | ||||
|         Reference = new OpenApiReference() | ||||
|         { | ||||
|             Id = "jwt_auth", | ||||
|             Type = ReferenceType.SecurityScheme | ||||
|         } | ||||
|     }; | ||||
|  | ||||
|     OpenApiSecurityRequirement securityRequirements = new OpenApiSecurityRequirement() | ||||
|     { | ||||
|         {securityScheme, new string[] { }}, | ||||
|     }; | ||||
|  | ||||
|     options.AddSecurityRequirement(securityRequirements); | ||||
| }); | ||||
|  | ||||
| builder.Services.AddDbContext<UsersAccessDbContext>(option => | ||||
| { | ||||
|     option.UseSqlServer("UsersAccessConnection"); | ||||
| }); | ||||
| var app = builder.Build(); | ||||
|  | ||||
| // Configure the HTTP request pipeline. | ||||
| if (app.Environment.IsDevelopment()) | ||||
| { | ||||
|     app.UseSwagger(); | ||||
|     app.UseSwaggerUI(); | ||||
|  | ||||
|     using (var serviceScope = app.Services.CreateScope()) | ||||
|     { | ||||
|         var services = serviceScope.ServiceProvider; | ||||
|         var usersAccessProvider = services.GetRequiredService<IUsersAccessProvider>(); | ||||
|         usersAccessProvider.seedData(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| app.UseAuthentication(); | ||||
| app.UseAuthorization(); | ||||
|  | ||||
| app.MapControllers(); | ||||
| app.Run(); | ||||
| @ -0,0 +1,31 @@ | ||||
| { | ||||
|   "$schema": "https://json.schemastore.org/launchsettings.json", | ||||
|   "iisSettings": { | ||||
|     "windowsAuthentication": false, | ||||
|     "anonymousAuthentication": true, | ||||
|     "iisExpress": { | ||||
|       "applicationUrl": "http://localhost:28382", | ||||
|       "sslPort": 0 | ||||
|     } | ||||
|   }, | ||||
|   "profiles": { | ||||
|     "DamageAssesment.Api.Users": { | ||||
|       "commandName": "Project", | ||||
|       "dotnetRunMessages": true, | ||||
|       "launchBrowser": true, | ||||
|       "launchUrl": "swagger", | ||||
|       "applicationUrl": "http://localhost:5027", | ||||
|       "environmentVariables": { | ||||
|         "ASPNETCORE_ENVIRONMENT": "Development" | ||||
|       } | ||||
|     }, | ||||
|     "IIS Express": { | ||||
|       "commandName": "IISExpress", | ||||
|       "launchBrowser": true, | ||||
|       "launchUrl": "swagger", | ||||
|       "environmentVariables": { | ||||
|         "ASPNETCORE_ENVIRONMENT": "Development" | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,305 @@ | ||||
| using AutoMapper; | ||||
| using DamageAssesment.Api.UsersAccess.Db; | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.Extensions.Options; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
| using System.Data; | ||||
| using System.IdentityModel.Tokens.Jwt; | ||||
| using System.Security.Claims; | ||||
| using System.Text; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Providers | ||||
| { | ||||
|     public class UsersAccessProvider : IUsersAccessProvider | ||||
|     { | ||||
|         private readonly UsersAccessDbContext userAccessDbContext; | ||||
|         private readonly ILogger<UsersAccessProvider> logger; | ||||
|         private readonly IMapper mapper; | ||||
|         //private readonly IEmployeeServiceProvider employeeServiceProvider; | ||||
|         private readonly JwtSettings jwtSettings; | ||||
|         private readonly ITokenServiceProvider tokenServiceProvider; | ||||
|  | ||||
|         public UsersAccessProvider(IOptions<JwtSettings> options, ITokenServiceProvider tokenServiceProvider, UsersAccessDbContext userAccessDbContext, IEmployeeServiceProvider employeeServiceProvider, ILogger<UsersAccessProvider> logger, IMapper mapper) | ||||
|         { | ||||
|             this.userAccessDbContext = userAccessDbContext; | ||||
|             //this.employeeServiceProvider = employeeServiceProvider; | ||||
|             this.logger = logger; | ||||
|             this.mapper = mapper; | ||||
|             jwtSettings = options.Value; | ||||
|             this.tokenServiceProvider = tokenServiceProvider; | ||||
|            // seedData(); | ||||
|         } | ||||
|  | ||||
|         public void seedData() | ||||
|         { | ||||
|             if (!userAccessDbContext.Users.Any()) | ||||
|             { | ||||
|                 userAccessDbContext.Users.Add(new Db.User { Id = 1, EmployeeId = 1, EmployeeCode = "Emp1", RoleId = 1, IsActive = true, CreateDate = DateTime.Now }); | ||||
|                 userAccessDbContext.Users.Add(new Db.User { Id = 2, EmployeeId = 2, EmployeeCode = "Emp2", RoleId = 2, IsActive = true, CreateDate = DateTime.Now }); | ||||
|                 userAccessDbContext.Users.Add(new Db.User { Id = 3, EmployeeId = 3, EmployeeCode = "Emp3", RoleId = 3, IsActive = true, CreateDate = DateTime.Now }); | ||||
|                 userAccessDbContext.SaveChanges(); | ||||
|             } | ||||
|  | ||||
|             if (!userAccessDbContext.Roles.Any()) | ||||
|             { | ||||
|                 userAccessDbContext.Roles.Add(new Db.Role { Id = 1, Name = "admin", Description ="Administrator role have full access" }); | ||||
|                 userAccessDbContext.Roles.Add(new Db.Role { Id = 2, Name = "user", Description =" User role"}); | ||||
|                 userAccessDbContext.Roles.Add(new Db.Role { Id = 3, Name = "survey", Description ="Survey role" }); | ||||
|                 userAccessDbContext.Roles.Add(new Db.Role { Id = 4, Name = "report", Description ="Report role"}); | ||||
|                 userAccessDbContext.Roles.Add(new Db.Role { Id = 5, Name = "document", Description ="Document role" }); | ||||
|                 userAccessDbContext.SaveChanges(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, IEnumerable<Models.User> Users, string ErrorMessage)> GetUsersAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Gell all Users from DB"); | ||||
|                 var users = await userAccessDbContext.Users.ToListAsync(); | ||||
|                 if (users != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"{users.Count} Items(s) found"); | ||||
|                     var result = mapper.Map<IEnumerable<Db.User>, IEnumerable<Models.User>>(users); | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
|                 return (false, null, "Not found"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.User User, string ErrorMessage)> GetUsersAsync(int Id) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Querying Users table"); | ||||
|                 var user = await userAccessDbContext.Users.SingleOrDefaultAsync(s => s.Id == Id); | ||||
|                 if (user != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"User Id: {Id} found"); | ||||
|                     var result = mapper.Map<Db.User, Models.User>(user); | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
|                 return (false, null, "Not found"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.User User, string ErrorMessage)> PostUserAsync(Models.User user) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (user != null) | ||||
|                 { | ||||
|                     var _user = mapper.Map<Models.User, Db.User>(user); | ||||
|                     userAccessDbContext.Users.Add(_user); | ||||
|                     user.Id = _user.Id; | ||||
|                     await userAccessDbContext.SaveChangesAsync(); | ||||
|                     return (true, user, "Successful"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     logger?.LogInformation($"null object cannot be added"); | ||||
|                     return (false, null, $"null object cannot be added"); | ||||
|                 } | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.User User, string ErrorMessage)> PutUserAsync(int Id, Models.User user) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (user != null) | ||||
|                 { | ||||
|                     var _user = await userAccessDbContext.Users.AsNoTracking().Where(s => s.Id == Id).SingleOrDefaultAsync(); | ||||
|  | ||||
|                     if (_user != null) | ||||
|                     { | ||||
|                         int count = userAccessDbContext.Users.Where(u => u.Id != user.Id).Count(); | ||||
|                         if (count == 0) | ||||
|                         { | ||||
|                             await userAccessDbContext.SaveChangesAsync(); | ||||
|                             logger?.LogInformation($"Employee Id:  {user.EmployeeId} updated successfuly"); | ||||
|                             return (true, mapper.Map<Db.User, Models.User>(_user), $"Employee Id:  {_user.EmployeeId} updated successfuly"); | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             logger?.LogInformation($"Employee Id:  {user.EmployeeId} is already exist"); | ||||
|                             return (false, null, $"Employee Id:  {user.EmployeeId} is already exist"); | ||||
|                         } | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         logger?.LogInformation($"User Id : {Id} Not found"); | ||||
|                         return (false, null, "Not Found"); | ||||
|                     } | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     logger?.LogInformation($"User Id: {Id} Bad Request"); | ||||
|                     return (false, null, "Bad request"); | ||||
|                 } | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.User User, string ErrorMessage)> DeleteUserAsync(int Id) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var user = await userAccessDbContext.Users.Where(x => x.Id == Id).SingleOrDefaultAsync(); | ||||
|  | ||||
|                 if (user != null) | ||||
|                 { | ||||
|                     userAccessDbContext.Users.Remove(user); | ||||
|                     await userAccessDbContext.SaveChangesAsync(); | ||||
|                     logger?.LogInformation($"User Id: {Id} deleted Successfuly"); | ||||
|                     return (true, mapper.Map<Db.User, Models.User>(user), $"User Id: {Id} deleted Successfuly"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     logger?.LogInformation($"User Id : {Id} Not found"); | ||||
|                     return (false, null, "Not Found"); | ||||
|                 } | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, TokenResponse TokenResponse, string ErrorMessage)> AuthenticateAsync(string employecode) | ||||
|         { | ||||
|            | ||||
|             if (employecode != null) | ||||
|               { | ||||
|                 //implementation for dadeschools authentication | ||||
|                 // var employees = await employeeServiceProvider.getEmployeesAsync(); | ||||
|                 // var employee = employees.Where(e=> e.EmployeeCode.ToLower() == employecode.ToLower()).SingleOrDefault(); | ||||
|                 var user = userAccessDbContext.Users.Where(x => x.IsActive == true && x.EmployeeCode.ToLower() == employecode.ToLower()).SingleOrDefault(); | ||||
|         | ||||
|                           if (user != null) | ||||
|                           { | ||||
|  | ||||
|                               var r = await GetRolesAsync(); | ||||
|                               var role = r.Roles.Where(x => x.Id == user.RoleId).SingleOrDefault(); | ||||
|  | ||||
|                               var authClaims = new List<Claim> { | ||||
|                                new Claim(ClaimTypes.Name, user.EmployeeCode), | ||||
|                                new Claim(ClaimTypes.Role, role.Name), | ||||
|                                new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString()) | ||||
|  | ||||
|                               }; | ||||
|  | ||||
|                               /// Generate Token | ||||
|                               var tokenhandler = new JwtSecurityTokenHandler(); | ||||
|                               var tokenkey = Encoding.UTF8.GetBytes(jwtSettings.securitykey); | ||||
|                               var tokendesc = new SecurityTokenDescriptor | ||||
|                               { | ||||
|                                   Audience = "", | ||||
|                                   NotBefore = DateTime.Now, | ||||
|                                   Subject = new ClaimsIdentity(authClaims), | ||||
|                                   Expires = DateTime.Now.AddMinutes(30), | ||||
|                                   SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(tokenkey), SecurityAlgorithms.HmacSha256) | ||||
|                               }; | ||||
|                               var token = tokenhandler.CreateToken(tokendesc); | ||||
|                               string finaltoken = tokenhandler.WriteToken(token); | ||||
|  | ||||
|                               var response = new TokenResponse() { jwttoken = finaltoken, refreshtoken = await tokenServiceProvider.GenerateToken(mapper.Map<Db.User,Models.User>(user)) }; | ||||
|                               return (true, response, "Authentication success and token issued."); | ||||
|                           } | ||||
|                           else | ||||
|                           { | ||||
|                               return (false, null, "user inactive or not exist."); | ||||
|                           } | ||||
|                   } | ||||
|  | ||||
|               else | ||||
|               { | ||||
|                   return (false, null, "Credentials are required to authenticate."); | ||||
|               } | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, IEnumerable<Models.Role> Roles, string ErrorMessage)> GetRolesAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Gell all Roles from DB"); | ||||
|                 var roles = await userAccessDbContext.Roles.ToListAsync(); | ||||
|                 if (roles != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"{roles.Count} Items(s) found"); | ||||
|                     var result = mapper.Map<IEnumerable<Db.Role>, IEnumerable<Models.Role>>(roles); | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
|                 return (false, null, "Not found"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.TokenResponse TokenResponse, string ErrorMessage)> RefreshTokenAsync(TokenResponse tokenResponse) | ||||
|         { | ||||
|             //Generate token | ||||
|             var tokenhandler = new JwtSecurityTokenHandler(); | ||||
|             var tokenkey = Encoding.UTF8.GetBytes(this.jwtSettings.securitykey); | ||||
|             SecurityToken securityToken; | ||||
|             var principal = tokenhandler.ValidateToken(tokenResponse.jwttoken, new TokenValidationParameters | ||||
|             { | ||||
|                 ValidateIssuerSigningKey = true, | ||||
|                 IssuerSigningKey = new SymmetricSecurityKey(tokenkey), | ||||
|                 ValidateIssuer = false, | ||||
|                 ValidateAudience = false, | ||||
|  | ||||
|             }, out securityToken); | ||||
|  | ||||
|             var token = securityToken as JwtSecurityToken; | ||||
|             if (token != null && !token.Header.Alg.Equals(SecurityAlgorithms.HmacSha256)) | ||||
|             { | ||||
|                 return (false, null, "Unauthorized"); | ||||
|             } | ||||
|             var username = principal.Identity?.Name; | ||||
|  | ||||
|             var tokens = await userAccessDbContext.Tokens.ToListAsync(); | ||||
|             var users = await userAccessDbContext.Users.ToListAsync(); | ||||
|  | ||||
|             var user = (from u in users | ||||
|                         join t in tokens | ||||
|                         on u.Id equals t.UserId | ||||
|                         where u.EmployeeId == 1 | ||||
|                         && t.RefreshToken == tokenResponse.refreshtoken | ||||
|                         select u).FirstOrDefault(); | ||||
|  | ||||
|             if (user == null) | ||||
|                 return (false, null, "Invalid Token Response object provided"); | ||||
|  | ||||
|             var _user = mapper.Map<Db.User, Models.User>(user); | ||||
|             var response = tokenServiceProvider.TokenAuthenticate(_user, principal.Claims.ToArray()).Result; | ||||
|             return (true, response, "Token authenticated and refreshed."); | ||||
|         } | ||||
|  | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,50 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Services | ||||
| { | ||||
|     public class EmployeeServiceProvider : ServiceProviderBase, IEmployeeServiceProvider | ||||
|     { | ||||
|         public EmployeeServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Employee"), configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase")) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Employee>> getEmployeesAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null); | ||||
|                 var employees = JsonConvert.DeserializeObject<List<Employee>>(responseJsonString); | ||||
|  | ||||
|                 if (employees == null || !employees.Any()) | ||||
|                     return new List<Employee>(); | ||||
|                 else return employees; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeesAsync()"); | ||||
|                 return new List<Employee>(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<Employee> getEmployeeAsync(int employeeId) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:EmployeeById"), employeeId); | ||||
|                 var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null); | ||||
|                 var employee = JsonConvert.DeserializeObject<Employee>(responseJsonString); | ||||
|  | ||||
|                 if (employee == null) | ||||
|                     return null; | ||||
|                 else return employee; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeeAsync()"); | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,42 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
| using System.Net.Http.Headers; | ||||
| using System.Text; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Services | ||||
| { | ||||
|     public class HttpUtil : IHttpUtil | ||||
|     { | ||||
|         private readonly HttpClient httpClient; | ||||
|         private readonly ILogger<HttpUtil> logger; | ||||
|  | ||||
|         public HttpUtil(HttpClient httpClient, ILogger<HttpUtil> logger) | ||||
|         { | ||||
|             this.httpClient = httpClient; | ||||
|             this.logger = logger; | ||||
|         } | ||||
|         public async Task<string> SendAsync(HttpMethod method, string url, string JsonInput) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var request = new HttpRequestMessage(method, url); | ||||
|                 request.Headers.Accept.Clear(); | ||||
|                 request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||||
|  | ||||
|                 //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); | ||||
|                 if (method == HttpMethod.Post) | ||||
|                 { | ||||
|                     request.Content = new StringContent(JsonInput, Encoding.UTF8, "application/json"); | ||||
|                 } | ||||
|                 var response = await httpClient.SendAsync(request, CancellationToken.None); | ||||
|                 response.EnsureSuccessStatusCode(); | ||||
|                 var responseString = await response.Content.ReadAsStringAsync(); | ||||
|                 return responseString; | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError($"Exception Message : {ex.Message} - Ref: HttpUtil.SendAsync()"); | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,25 @@ | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Services | ||||
| { | ||||
|     public class ServiceProviderBase | ||||
|     { | ||||
|         protected readonly IConfiguration configuration; | ||||
|         protected readonly IHttpUtil httpUtil; | ||||
|         protected readonly ILogger<ServiceProviderBase> logger; | ||||
|         protected string ressource; | ||||
|         protected string urlBase; | ||||
|         protected string url; | ||||
|          | ||||
|  | ||||
|         public ServiceProviderBase(IConfiguration configuration, IHttpUtil httpUtil, ILogger<ServiceProviderBase> logger, string ressource, string urlBase) | ||||
|         { | ||||
|             this.configuration = configuration; | ||||
|             this.httpUtil = httpUtil; | ||||
|             this.logger = logger; | ||||
|             this.ressource = ressource; | ||||
|             this.urlBase = urlBase; | ||||
|             url = urlBase + ressource; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,59 @@ | ||||
| using System.IdentityModel.Tokens.Jwt; | ||||
| using System.Security.Claims; | ||||
| using System.Security.Cryptography; | ||||
| using System.Text; | ||||
| using DamageAssesment.Api.UsersAccess.Db; | ||||
| using DamageAssesment.Api.UsersAccess.Interfaces; | ||||
| using DamageAssesment.Api.UsersAccess.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.Extensions.Options; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
|  | ||||
| namespace DamageAssesment.Api.UsersAccess.Services | ||||
| { | ||||
|     public class TokenServiceProvider : ITokenServiceProvider | ||||
|     { | ||||
|         private readonly UsersAccessDbContext usersAccessDbContext; | ||||
|         private readonly JwtSettings jwtSettings; | ||||
|         public TokenServiceProvider(IOptions<JwtSettings> options, UsersAccessDbContext usersAccessDbContext) | ||||
|         { | ||||
|             this.usersAccessDbContext = usersAccessDbContext; | ||||
|             this.jwtSettings = options.Value; | ||||
|         } | ||||
|         public async Task<string> GenerateToken(Models.User user) | ||||
|         { | ||||
|             var randomnumber = new byte[32]; | ||||
|             using (var ramdomnumbergenerator = RandomNumberGenerator.Create()) | ||||
|             { | ||||
|                 ramdomnumbergenerator.GetBytes(randomnumber); | ||||
|                 string refreshtoken = Convert.ToBase64String(randomnumber); | ||||
|                 var token = await usersAccessDbContext.Tokens.FirstOrDefaultAsync(item => item.UserId == user.Id); | ||||
|                 if (token != null) | ||||
|                 { | ||||
|                     token.RefreshToken = refreshtoken; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     usersAccessDbContext.Tokens.Add(new Db.Token() | ||||
|                     { | ||||
|                         UserId = user.Id, | ||||
|                         RefreshToken = refreshtoken, | ||||
|                         IsActive = true | ||||
|                     }); | ||||
|                 } | ||||
|                 await usersAccessDbContext.SaveChangesAsync(); | ||||
|  | ||||
|                 return refreshtoken; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<TokenResponse> TokenAuthenticate(Models.User user, Claim[] claims) | ||||
|         { | ||||
|             var token = new JwtSecurityToken(claims: claims, expires: DateTime.Now.AddSeconds(20), | ||||
|               signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.securitykey)), SecurityAlgorithms.HmacSha256) | ||||
|             ); | ||||
|             var jwttoken = new JwtSecurityTokenHandler().WriteToken(token); | ||||
|             return new TokenResponse() { jwttoken = jwttoken, refreshtoken = await GenerateToken(user) }; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,8 @@ | ||||
| { | ||||
|   "Logging": { | ||||
|     "LogLevel": { | ||||
|       "Default": "Information", | ||||
|       "Microsoft.AspNetCore": "Warning" | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,41 @@ | ||||
| { | ||||
|   "JwtSettings": { | ||||
|     "securitykey": "bWlhbWkgZGFkZSBzY2hvb2xzIHNlY3JldCBrZXk=" | ||||
|   }, | ||||
|   "Logging": { | ||||
|     "LogLevel": { | ||||
|       "Default": "Information", | ||||
|       "Microsoft.AspNetCore": "Warning" | ||||
|     } | ||||
|   }, | ||||
|   "EndPointSettings": { | ||||
|     "EmployeeUrlBase": "http://localhost:5135" | ||||
|   }, | ||||
|   "RessourceSettings": { | ||||
|     "Employee": "/Employees", | ||||
|     "EmployeeById": "/Employees/{0}" | ||||
|   }, | ||||
|   "AllowedHosts": "*", | ||||
|   "Dadeschools": { | ||||
|     "Authority": "https://dev-graph.dadeschools.net", | ||||
|     "TokenUrl": "https://dev-graph.dadeschools.net/connect/token", | ||||
|     "ClientId": "dmapi", | ||||
|     "ClientSecret": "bfce2c8d-2064-4a02-b19d-7f1d42b16eae", | ||||
|     "Name": "Dadeschools Identity Server" | ||||
|   }, | ||||
|   "Scopes": [ | ||||
|     { | ||||
|       "Name": "openid", | ||||
|       "Description": "Request an authentication token on your behalf" | ||||
|     }, | ||||
|     { | ||||
|       "Name": "profile", | ||||
|       "Description": "Read basic information about you such as your date of brith and full name" | ||||
|     } | ||||
|   ], | ||||
|   "ConnectionStrings": { | ||||
|     //"UsersAccessConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" | ||||
|     "UsersAccessConnection": "Server=tcp:da-dev.database.windows.net,1433;Initial Catalog=da-dev-db;Encrypt=True;User ID=admin-dev;Password=b3tgRABw8LGE75k;TrustServerCertificate=False;Connection Timeout=30;" | ||||
|  | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user