예제 #1
0
        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);
        }
예제 #2
0
        public BaseModel Logout()
        {
            var user = ClaimHelpers.GetUserFromClaims(User.Claims.ToArray());

            _authService.LogoffUser(user.UserId, user.LogId);
            return(new BaseModel()
            {
                Error = false
            });
        }
예제 #3
0
 public MealController(IHttpContextAccessor contextAccessor, IMealService mealService)
 {
     _mealService  = mealService;
     _loggedInUser = ClaimHelpers.GetUserFromClaims(contextAccessor.HttpContext.User.Claims);
 }
예제 #4
0
 public UserSettingsController(IHttpContextAccessor contextAccessor, IUserSettingsService userSettingsService)
 {
     _userSettingsService = userSettingsService;
     _loggedInUser        = ClaimHelpers.GetUserFromClaims(contextAccessor.HttpContext.User.Claims);
 }