1

The error is this:

Severity Code Description Project File Line Suppression State Error CS1061 'HttpRequest' does not contain a definition for 'ServerVariables' and no accessible extension method 'ServerVariables' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?) Elearn C:\Users\Isaac Bredbenner\source\repos\Elearm_Finalchanges\Elearn\Elearn\Controllers\AccountController.cs 161 Active

[HttpPost]
public IActionResult CookieConsents()
{
    CookieConsents cookieConsents = new CookieConsents
    {
       Consent = true,
       IpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"],
       ConsentReceivedOn = DateTime.Now
    };
    _elearnContext.CookieConsents.Add(cookieConsents);
    _elearnContext.SaveChanges();
    return View();
}

I have been trying to research this error and I found a form that was telling me to install a package but even after installing it on the visual studio I am getting this error. my file imports are as follows:

using DAL.Context;
using DAL.Models;
using DAL.Resources;
using Elearn.Helpers;
using Elearn.Infrastructure;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using X.PagedList;

Do I need to be using httpRequest or something along those lines? please let me know if anyone has any ideas on what the root of my problem is.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • The HttpRequest is part of System.Web, so you should be good there. This is a good read, and may better your understanding - https://totaluptime.com/kb/getting-the-original-client-ip-with-x-forwarded-for-in-your-code/. – Nelson Mar 16 '21 at 15:58

1 Answers1

2

If you are using .net core, then it isn't available: try this link

The requested page is not available for .NET Core 3.1. You have been redirected to the newest product version this page is available for.

What you can do though is get the relevant information from ConnectionInfo

RemoteIpAddress

HttpContext.Connection.RemoteIpAddress;

Detailed information on accessing the context can be found here: How to get Client IP address in ASP.NET Core 2.1

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Bear in mind that RemoteIpAddress may not be valid (or could be localhost, ::1), if your server is sitting behind a load balancer. In which case, you should configure the LB to pass on the original HTTP_X_FORWARDED_FOR (or whatever property it is). – Neil Mar 16 '21 at 16:46