Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 11, 2018 1:11 PM
I am new to Core MVC.
What's the equivalent of Session_Start in Global.asax file ASP.Net MVC in CORE MVC?
I am looking for sample code.
Thanks
All replies (13)
Thursday, May 24, 2018 8:41 AM ✅Answered
Hi klouapple,
For accessing session from razor view, you could try code below:
@using Microsoft.AspNetCore.Http;
@inject IHttpContextAccessor HttpContextAccessor
@{
ViewData["Title"] = "Index";
string Name = HttpContextAccessor.HttpContext.Session.GetString("_Name");
}
<h4>Index</h4>
@Name
Best Regards,
Edward
Friday, May 11, 2018 2:06 PM
There isn’t one. You can add middleware, check a value in session, or write your own session provider.
Note: unlike the old session manager, in session is only fetched if you access it, and only written if you update/add a value.
Wednesday, May 16, 2018 6:03 PM
Hi bruce,
Thank so much for the help.
In asp.net web form and mvc apps, I use Session_Start to get the login users information, such as phone, title, name, etc.. save it into a session variable so it can be used in all of pages.
Now I lost in core mvc, Do you have any sample code?
Thanks
Wednesday, May 16, 2018 6:37 PM
klouapple
In asp.net web form and mvc apps, I use Session_Start to get the login users information, such as phone, title, name, etc.. save it into a session variable so it can be used in all of pages.
Now I lost in core mvc, Do you have any sample code?
Thanks
Modern application use claims to persist this type of information. ASP Identity API has a claims store and can be plugged into ASP Core via middleware and DI as mentioned above.
/en-us/aspnet/core/security/authorization/claims?view=aspnetcore-2.0
The ASP Identity API caches claims when the user logs in so phone, title, name, etc.. are available in every request.
Friday, May 18, 2018 12:15 PM
Thank you for the replay mgebhard,
For example, I need to save the user login date time every time the user access to the web site, I use Session_Start to save the date time into database in .net form and mvc apps.
.net core does not have Session_Start, Where and how can I do the same thing? this just saves date time when the user session starts, not every request.
If we use the middle ware, the middle ware will be executed on every request, instead of just user session start?
Thanks
Friday, May 18, 2018 1:03 PM
Basically, your app tracks a when a Session cookie is created. Do the same in the Core App. If the request does not have the tracking cookie create the cookie and write a record to the DB. This can be done in middleware.
Is there any reason why you don't simply write the login date when the user logs in? Seems like login is best place to track login time rather than tracking a separate cookie expiration like you're doing now. Perhaps explain the use case so we can better point you in the right design direction.
Read the ASP Core fundamental documentation to learn the features ASP Core has to offer.
/en-us/aspnet/core/fundamentals/?view=aspnetcore-2.0&tabs=aspnetcore2x
Friday, May 18, 2018 2:55 PM
The web application is intranet by using windows authentication, There is no log on page.
I use Session_Start to get user information from database and save it in session variable so the session variable can be used in the user session, all of pages.
Friday, May 18, 2018 3:22 PM
unlike old asp.net, asp.net core does not load session each request, only if requested during the request. also you should be using the async version of session. in your case, it would be better to have a session accessor, that checks session, if not there loads session. just make a session extension method:
public static Task<UserInfo> GetUserInfoAsync(this ISession session, string userName) {....}
....
var userInfo = await HttpContext.Session.GetUserInfoAsync(userName);
Friday, May 18, 2018 4:00 PM
bruce, Thank so much for the help,
Basically I do in .net web form and standard mvc is the following, How to achieve it in .net core? Is it possible to send me sample code,
In global.asax file
void Session_Start(object sender, EventArgs e)
{
Session["UserInfo"] = getuserInfoFromUserTable()
SaveLogonDateTime(currentUser);
}
In other controller, UserInfor can be used, instead of get it from table again
public class TestController : Controller
{
public ActionResult Index(string picturePathName)
{
UserInfor _userInfo = Session["UserInfo"] as UserInfor;
return View();
}
}
Friday, May 18, 2018 5:56 PM
Session state is a feature in ASP.NET Core that you can use to save and store user data while the user browses your web app. Consisting of a dictionary or hash table on the server, session state persists data across requests from a browser. The session data is backed by a cache.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
app.UseMvcWithDefaultRoute();
}
Friday, May 18, 2018 5:59 PM
HttpModules have been replaced by middleware in ASP.Net 5
Session_Start and Application_BeginRequest are methods of global.asax which is an Http_Module.
So, to replace Application_BeginRequest you can write your own middleware.
By default, there isn't session in ASP.Net 5. You must add and configure the session middleware in your Startup to use it.
Wednesday, May 23, 2018 6:31 PM
Thank you so much for the link
I download the sample from https://github.com/aspnet/Docs/tree/master/aspnetcore/fundamentals/app-state/sample/src/WebAppSessionDotNetCore2.0App
For example, the HttpContext.Session.SetString(SessionKeyName, "Rick") can be accessed in entire application during the user session,
to get the value by using HttpContext.Session.GetString(SessionKeyName);
But how to get the its value in razor, I get error from the code below, I need show a login user name in the _Layout.cshtml,
@using Microsoft.AspNetCore.Http;
@{
ViewData["Title"] = "Index";
string Name = HttpContext.Session.GetString("_Name");
}
<h4>Index</h4>
@Name
Thursday, May 24, 2018 12:47 PM
Thanks so much Edward, it resolve the issue.