简体   繁体   中英

How to set global data in startup.cs upon logging in .net core 3.1 web application?

My .net core 3.1 web application starts with the user having to SignIn, along with selecting the Company/Branch (CompanyCode). Now how and where do i save this application data 'CompanyCode', for use in rest of the session. I have figured out a way to set other static Global variables (constants) in the startup.cs file.

 public void ConfigureServices(IServiceCollection services)
        {
            Action<GlobalData> gData = (g =>
            {
                g.CompanyCode = 2;
                g.DummyCust = 3;
                g.Type_Personal = 0;
                g.Type_Asset = 1;
                g.Type_Bank = 2;

                g.Type_Customer = 1;
                g.Type_Staff = 2;

            });
            services.Configure(gData);
            services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<GlobalData>>().Value);

            services.AddControllersWithViews();

            services.AddDbContext<ImgContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ImgContext")));
        }

GlobalData Class Definition:

namespace Imagetech.Data
{
    public class GlobalData
    {
        public int CompanyCode { get; set; }
        public int Type_Personal { get; set; }
        public int Type_Asset { get; set; }
        public int Type_Bank { get; set; }
        public int Type_Customer { get; set; }
        public int Type_Staff { get; set; }
        public int DummyCust { get; set; }
    }
}

Now CompanyCode being a dynamic Global data to be set after login; how can i achieve it ? Pls do help. Thanks

The best, and easiest way to do this is

  1. Upon successful login, return the companyCode to caller

  2. Caller (may be a javascript app, will store the companyCode globally eg localstorage.)

  3. Have the caller include the companyCode in request headers.

  4. That way you can just read the headers and get the company code.

A few ways to make this easier -

  • use JWT for authentication, that way you can simply include the companyCode in your token, and nobody can tamper with it.

  • Store the companyCode in httpOnly cookie upon login. Cookies are then automatically included in every request to your webapp. Of course for this solution to work you'll need to ask your users to have cookies enabled (which is okay, many websites do.)

Now how and where do i save this application data 'CompanyCode', for use in rest of the session.

You could also use DI in your login controller/razor pages to get and set the CompanyCode , for example

public class AccountController : Controller
{
    private readonly IOptions<GlobalData> _globalData;

    public AccountController(IOptions<GlobalData> globalData)
    {
        _globalData = globalData;
    }

    [HttpPost]
    public IActionResult Login()
    {
       var originalData = _globalData.Value.CompanyCode;
       //after login successfully
       //replace below 100 with your selected `CompanyCode`,
       _globalData.Value.CompanyCode = 100

        return View();
    }
}

Refer to Global Variables in ASP.Net Core 2

一种快速的方法是将所有常量(假设这些值在开发过程中固定,而不是配置设置)集中在一个静态类中

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