dimanche 17 avril 2022

Access "User.Identities" and "HttpContext.Session" out of controller class Net Core C#

I am rewriting a Net Application from dotNet 4 to Core6.

Now I am facing the problem of how to create a user identity class which can be access by the get methods in the Core6 controller class.

AS both "User.Identities" and "HttpContext.Session" depends on controller class and controller is single inheritance(inherits single class).

So i can't move both "User.Identities" and "HttpContext.Session" out of controller class, and the controller class is single inheritance as well.

Are there any ways to access the following method in every controller of my application

Thanks.

   public string getCurrentUser()
    {
                  
        string username="none";
        if 
        {
            username = User.Identities.SingleOrDefault().Name;
        }


        if (HttpContext.Session.GetString("userid") != null)
        {
            username = (HttpContext.Session.GetString("userid"));
        }

        return username;
    }

Aucun commentaire:

Enregistrer un commentaire