0

I have problem in my LoggedUser property which is made back in the days with .Net Framework (standart). Now I want to fix LoggedUser, but using Asp.Net Core. The error that I got is that I can't use HttpContext.Current.Session it underline Current. Who could help me to fix this and save the same structure of code. Thank You!

public class AuthenticationService
    {
        public static bool IsLogged
        {
            get
            {
                return LoggedUser != null;
            }
        }

        public static User LoggedUser
        {
            get
            {
                return (User)HttpContext.Current.Session[typeof(AuthenticationService).Name];
            }
            private set
            {
                HttpContext.Current.Session[typeof(AuthenticationService).Name] = value;
            }
        }

        public static void AuthenticateUser(string username, string password)
        {
            UserRepository userRepo = new UserRepository();

            User user = userRepo.GetAll(u => u.Username == username && u.Password == password).FirstOrDefault();

            if (user != null)
                LoggedUser = user;
        }

        public static void LogOut()
        {
            LoggedUser = null;
        }
    }
  • Does this answer your question? [Access the current HttpContext in ASP.NET Core](https://stackoverflow.com/questions/31243068/access-the-current-httpcontext-in-asp-net-core) – phuzi Nov 18 '20 at 11:13
  • Sorry, can't figure it out. I'm newbie at this. Don't know how to do it properly. – Kiril Vodenicharov Nov 18 '20 at 11:22
  • Put you check/paste your startup.cs file and ensure you have sessions enabled and also adding HttpContext service? [Read This](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0) – AliK Nov 18 '20 at 12:05
  • in `Startup.cs` I've added this ` services.AddHttpContextAccessor()` but still don't know how to make it and save the same structure in AuthService... – Kiril Vodenicharov Nov 18 '20 at 12:12
  • Do you understand how Dependency Injection works? – mason Nov 18 '20 at 15:47

0 Answers0