public static IServiceCollection AddJwtAuthentication( this IServiceCollection services, AuthenticationConfiguration configuration) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, IssuerSigningKey = configuration.GetSymmetricSecurityKey(), ValidateIssuerSigningKey = true }; options.Events = new JwtBearerEvents { OnMessageReceived = context => { var accessToken = context.Request.Query[JwtQueryParameter].ToString() ?? context.Request.GetJwtToken(); var path = context.HttpContext.Request.Path; if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments(SignalRBaseSegment)) { context.Token = accessToken; } return(Task.CompletedTask); } }; }); return(services); }
private UserTokenDto GenerateToken(GetUserDto user) { Claim[] claims = new[] { new Claim(nameof(User.Id), user.Id), new Claim(nameof(User.Email), user.Email), new Claim(JwtRegisteredClaimNames.Nbf, new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString()), new Claim(JwtRegisteredClaimNames.Exp, new DateTimeOffset(DateTime.Now.AddDays(_configuration.TokenExpirationPeriodInDay)).ToUnixTimeSeconds().ToString()), }; JwtSecurityToken token = new JwtSecurityToken( new JwtHeader(new SigningCredentials(_configuration.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)), new JwtPayload(claims)); string tokenValue = new JwtSecurityTokenHandler().WriteToken(token); UserTokenDto tokenResponse = new UserTokenDto { User = user, AccessToken = tokenValue, ExpiresIn = (int)TimeSpan.FromDays(_configuration.TokenExpirationPeriodInDay).TotalSeconds }; return(tokenResponse); }
public static IServiceCollection AddJwtAuthentication( this IServiceCollection services, AuthenticationConfiguration configuration) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, IssuerSigningKey = configuration.GetSymmetricSecurityKey(), ValidateIssuerSigningKey = true }; }); return(services); }
private string GenerateToken(GetUserDto user) { var claims = new[] { new Claim(nameof(Domain.Users.User.Id), user.Id), new Claim(nameof(Domain.Users.User.Email), user.Email), new Claim(nameof(Domain.Users.User.Interests), user.Interests ?? string.Empty), new Claim(ClaimTypes.Role, user.Role), new Claim(JwtRegisteredClaimNames.Nbf, new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString()), new Claim(JwtRegisteredClaimNames.Exp, new DateTimeOffset(DateTime.Now.AddDays(1)).ToUnixTimeSeconds().ToString()), }; var token = new JwtSecurityToken( new JwtHeader(new SigningCredentials(_configuration.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)), new JwtPayload(claims)); return(new JwtSecurityTokenHandler().WriteToken(token)); }