public static IServiceCollection RegisterJwtAuthentication(this IServiceCollection services, string secret, bool requireHttpsMetadata) { var key = Encoding.ASCII.GetBytes(secret); services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(x => { x.Events = new JwtBearerEvents { OnTokenValidated = ctx => { var loginUser = ClaimHelpers.GetUserFromClaims(ctx.Principal.Claims.ToArray()); var authService = ctx.HttpContext.RequestServices.GetRequiredService <IAuthService>(); if (!authService.CheckLogin(loginUser.UserId, loginUser.LogId)) { ctx.Fail($"User is no longer logged in."); } return(Task.CompletedTask); } }; x.RequireHttpsMetadata = requireHttpsMetadata; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, }; }); return(services); }
public BaseModel Logout() { var user = ClaimHelpers.GetUserFromClaims(User.Claims.ToArray()); _authService.LogoffUser(user.UserId, user.LogId); return(new BaseModel() { Error = false }); }
public MealController(IHttpContextAccessor contextAccessor, IMealService mealService) { _mealService = mealService; _loggedInUser = ClaimHelpers.GetUserFromClaims(contextAccessor.HttpContext.User.Claims); }
public UserSettingsController(IHttpContextAccessor contextAccessor, IUserSettingsService userSettingsService) { _userSettingsService = userSettingsService; _loggedInUser = ClaimHelpers.GetUserFromClaims(contextAccessor.HttpContext.User.Claims); }