1. Microservice and design principle and design patterns
2. 12 Factor APP Microservice and their App development
3. SOLID design principle
4. Security Machnisam ALL ( SAML, 2F AUTH , Password-based, OAUTH JWT ... ETC)
6. What JWT Token
7. Web API
8. Aync and Wait
9. How to create /Migrate increase to a Cloud environment
10 . How to Improve the performance of applications
11. How to start with Big application with start creating architecture that support concurrent user more 1 millions
5. How implemented JWT BASED AUTH :
| JWT Based Auth | Lib(using System.IdentityModel.Tokens.Jwt; using System.Security.Claims;using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Caching.Distributed; |
| public async Task<ActionResult<LoginResponse>> Login([FromBody] LoginRequest loginRequest) { LoginResponse loginResponse = await _authService.GenerateToken(loginRequest); return StatusCode(StatusCodes.Status200OK, loginResponse); } |
| public async Task<LoginResponse> GenerateToken(LoginRequest loginRequest) { IdentityUser user = await GetIdentityUser(loginRequest.Email); Account account = await GetUserAccount(loginRequest.Email); SignInResult result = await _signInManager.PasswordSignInAsync( loginRequest.Email, loginRequest.Password, false, false ); if (!result.Succeeded) { throw new UnauthorizedException(StaticValues.ErrorIncorrectCredentials); }
return await GetAuthTokens(user, account); } |
| private async Task<LoginResponse> GetAuthTokens(IdentityUser user, Account account) { IList<string>? userRoles = await _userManager.GetRolesAsync(user); ClaimsIdentity claimsIdentity = new(new[] { new Claim(StaticValues.EmailClaim, user.UserName) }); foreach (string userRole in userRoles) { claimsIdentity.AddClaim(new Claim(StaticValues.UserTypeClaim, userRole)); }
string token = _tokenConfig.GetJwtToken(claimsIdentity); string refreshToken = _tokenConfig.GetRefreshToken(); LoginResponse loginResponse = new() { AccessToken = token, RefreshToken = refreshToken }; DistributedCacheEntryOptions options = new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1) };
await _cache.SetStringAsync($"{account.Id}_{StaticValues.RefreshTokenKey}", refreshToken, options); return loginResponse; } |
| public string GetJwtToken(ClaimsIdentity claimsIdentity) |
| { |
| byte[] key = Convert.FromBase64String(_configuration["JwtSecret"]); |
| int jwtExpiry= Convert.ToInt32(_configuration["JwtTokenExpiryInDays"]); |
| JwtSecurityTokenHandler tokenHandler = new(); |
| SecurityTokenDescriptor tokenDescriptor = new() |
| { |
| Subject = claimsIdentity, |
| Expires = DateTime.UtcNow.AddDays(jwtExpiry), |
| Issuer = _configuration["JwtIssuer"], |
| Audience = _configuration["JwtAudience"], |
| SigningCredentials = |
| new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) |
| }; |
| SecurityToken? token = tokenHandler.CreateToken(tokenDescriptor); |
| return tokenHandler.WriteToken(token); |
| } |
| using ComponentSpace.Saml2; | SAML based authentications -> multitenant (KPMG, some client ) |
| |
| |
| |
|
-----------------SAML BASED AUTH -------------
No comments:
Post a Comment