简体   繁体   中英

Using static variables in C# Web Application - Accessing Session variables in class properties

Forgive me for my lack of coding knowledge as well as ability to ask the right question.

I'm rather new to this ASP.Net Web Application thing (Core), yet I still wondered..

In my current application, I have a class that has a property in which it gets it from a static variable, set when a user requests a controller. So the flow is: User sends a request with a variable in body, if not specified in body, the StaticClass.StaticProperty (example) is then set to the variable the user specified in the body (or default = 0), data is returned based upon the variable. Yet I wondered, since there is no thread guarantee on this variable, whether or not this could be changed or messed up when the web application gets 50,000 requests at once?

I looked into sessions and tried the following:

service.AddSession(); //Not sure this even does anything?
HttpContext.Session.SetString //Setting this works in the controller, but I cant access it elsewhere by GetString
System.Web.HttpContext.Current.Session["test"] // Cant even access System.Web.Httpcontext, doesn't seem to exist.
HttpContext.Current //doesn't exist either
Session["test"] //doesn't exist either

Can I send a session over somewhere? I'm pretty lost.

Not sure if any of this made sense, I'll try to elaborate if needed.

Thank you in advance.

EDIT: Updated info.

I have added this to my startup.cs: services.AddSingleton();

        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.Cookie.HttpOnly = true;
        });

and

        app.UseSession();

Setting the Session variable: https://i.imgur.com/CY8rcdk.png

Using the Session variable: https://i.imgur.com/SuLJKzV.png

Variable is always null.

Thank you for trying to help.

HttpContext is accessible only from things that are request specific, since it's a context of one and only request. And new controller instances are created by the framework for each request, with injected HttpContext. It's the developers job to pass it further if the need arises.

I recommend reading this article about it: https://dotnetcoretutorials.com/2017/01/05/accessing-httpcontext-asp-net-core/

First in your startup.cs, you need to register IHttpContextAccessor as a service like so :

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

When you create a helper/service class, you can then inject in the IHttpContextAccessor and use it. It would look like something not too dissimilar to this :

public class UserService : IUserService
{
  private readonly IHttpContextAccessor _httpContextAccessor;

  public UserService(IHttpContextAccessor httpContextAccessor)
  {
    _httpContextAccessor = httpContextAccessor;
  }

  public bool IsUserLoggedIn()
  {
    var context = _httpContextAccessor.HttpContext;
    return context.User.Identities.Any(x => x.IsAuthenticated);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM