0

I am migrating a project from .Net 4.6.2 into .Net Core 2.0. I am receiving error below. 'HttpContext' does not contain a definition for 'Current'

How would I resolve this?

Original Code:

public class CustomerAuthorize : AuthorizeAttribute
{
    /// Gets or sets a session variable indicates that the logged in user has been already authorized to login to the system. 
    private bool IsAuthorized
    {
        get
        {
            if (HttpContext.Current.Session["IsAuthorized"] != null &&

Error Code:

'HttpContext' does not contain a definition for 'Current'

Attempted Code:

public class CustomerAuthorize : AuthorizeAttribute
{
    // Gets or sets a session variable indicates that the logged in user has been already authorized to login to the system. 

    const string IsAuthorized2 = "IsAuthorized";
    private bool IsAuthorized
    {
        get
        {
            if (HttpContext.Session.GetString(IsAuthorized2) != null

Error:

An object reference is required for the non-static field, method, or property 'HttpContext.Session'

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
  • Possible duplicate of [How do you create a custom AuthorizeAttribute in ASP.NET Core?](https://stackoverflow.com/questions/31464359/how-do-you-create-a-custom-authorizeattribute-in-asp-net-core) – Llama Mar 21 '19 at 06:49
  • Also see: [Access the current HttpContext in ASP.NET Core](https://stackoverflow.com/questions/31243068/access-the-current-httpcontext-in-asp-net-core). – Llama Mar 21 '19 at 06:49
  • Here's a great answer: https://stackoverflow.com/a/38574489/3883866 – Jesse de Wit Mar 21 '19 at 06:50
  • 2
    Possible duplicate of [Access the current HttpContext in ASP.NET Core](https://stackoverflow.com/questions/31243068/access-the-current-httpcontext-in-asp-net-core) – Stefan Mar 21 '19 at 06:58
  • Might want to take a look at the SessionStorage object – bilpor Mar 21 '19 at 08:50

1 Answers1

1

Maybe you can use HttpContext.User.Identity.IsAuthenticated

Leocanis
  • 134
  • 1
  • 7
  • hmm, this is not working, is there another solution? –  Apr 20 '19 at 18:13
  • 2
    @JohnThomas You need to post your own question instead of adding a bounty to a question with an accepted answer. Apparently the accepted answer worked for OP, you're not going to get more/different answers here unless you post *why* this doesn't work for you. – Lasse V. Karlsen Apr 21 '19 at 20:30