Skip to content

Commit

Permalink
fix(User):add jwt class and service
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdijafariii committed Aug 12, 2024
1 parent c9ad77b commit 6c645b5
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
3 changes: 2 additions & 1 deletion AnalysisData/AnalysisData/AnalysisData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
</ItemGroup>

Expand Down
5 changes: 1 addition & 4 deletions AnalysisData/AnalysisData/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<UserRole> UserRoles { get; set; }

// public DbSet<SystemManager> SystemManagers { get; set; }
// public DbSet<DataManager> DataManagers { get; set; }
// public DbSet<DataAnalyst> DataAnalysts { get; set; }

}
12 changes: 3 additions & 9 deletions AnalysisData/AnalysisData/JwtService/JwtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,17 @@ public async Task<string> GenerateJwtToken(string userName)
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, userName),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};

Console.WriteLine("sadsada");
foreach (var role in roles)
{
var result = _roleRepository.GetRoles(role.Role.Id);
claims.Add(new Claim(ClaimTypes.Role, result.Result.RoleName));
var result = await _roleRepository.GetRoles(role.Id);
claims.Add(new Claim(ClaimTypes.Role, result.RoleName));
}


var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var token = new JwtSecurityToken(
issuer: _configuration["Jwt:Issuer"],
audience: _configuration["Jwt:Audience"],
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions AnalysisData/AnalysisData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
app.UseHttpsRedirection();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public UserRepository(ApplicationDbContext context)

public async Task<User> GetUser(string userName)
{
var test = await _context.Users.SingleOrDefaultAsync(x => x.Username == userName);
var test = await _context.Users.Include(u=> u.UserRoles).SingleOrDefaultAsync(x => x.Username == userName);
return test;
}

Expand Down
8 changes: 7 additions & 1 deletion AnalysisData/AnalysisData/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"Jwt": {
"CookieName" : "jwt",
"Key": "haskdhkashdsahdakjdhlkhdfkljshflkasz.ncv,mnz,m.cnvz.alsjdfla;aewfj;a;djf",
"ExpireMinutes": 60
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=MohaymenProject;Username=postgres;Password=mahdijm"
"DefaultConnection": "Host=localhost;Database=mohaymen;Username=postgres;Password=mahdijm;Timeout=300"
},

"AllowedHosts": "*"
}

0 comments on commit 6c645b5

Please sign in to comment.