title: ASP.NET Core Web API, How can I access HttpContext in startup class
body: <p>I'm trying to access <code>HttpContext</code> to get <code>RemoteIpAddress</code> and <code>User-Agent</code>, but within Startup.cs.</p>
  
  <pre><code> public class Startup
          {
              public Startup(IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
              {
                  Configuration = configuration;
                  _httpContextAccessor = httpContextAccessor;
              }
  
              public IConfiguration Configuration { get; }
              public IHttpContextAccessor _httpContextAccessor { get; }
  
              // This method gets called by the runtime. Use this method to add services to the container.
              public void ConfigureServices(IServiceCollection services)
              {
                  IdentityModelEventSource.ShowPII = true;
  
                  var key = Encoding.ASCII.GetBytes(Configuration.GetValue&lt;string&gt;("claveEncriptacion"));
                  var ip = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                  var userAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();
  
  services.AddAuthentication(x =&gt;
              {
                  x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                  x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
              }).AddJwtBearer(x =&gt;
              {
                  //x.Audience = ip + "-" + userAgent;
                  x.RequireHttpsMetadata = false;
                  x.SaveToken = true;
                  x.TokenValidationParameters = new TokenValidationParameters
                  {
                      ValidateIssuerSigningKey = true,
                      IssuerSigningKey = new SymmetricSecurityKey(key),
                      ValidateIssuer = false,
                      ValidateAudience = true
                  };
              });
  </code></pre>
  
  <p>With the previous code I have an error executing the project.</p>
  
  <pre><code>Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting to activate 'JobSiteMentorCore.Startup'.'
  </code></pre>