简体   繁体   中英

Accessing to User.Identity in Application_Start()

i need to access to User.Identity.Name in my Application_Start()

i will use this to get data from my DbContext

is it possible to do something like this? can i do better?

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        using (MyApp.ContextDB context = new MyApp.ContextDB())
        {
            var uid = context.SomeDBSet.Where(x => x.WinID == User.Identity.Name).FirstOrDefault();
            Application["uid"] = uid ;
        }
    }

There is no user in Application_Start .

Application_Start runs when setting up the application, before any requests come in.

It sounds like you actually want to set HttpContext state in Application_BeginRequest .
(not Application state, which is global and is shared among all requests)

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