Asp.net mvc keep the object alive, information

I have this code

[HttpPost] public ActionResult Index(LoginModel loginModel) { if (ModelState.IsValid) { // some lines of code . bla bla bla TempData["loginModel"] = loginModel; return RedirectToAction("index", "premium"); } ... } 

and this controller is here

 public ActionResult Index() { var loginModel = TempData["loginModel"] as LoginModel; ... } 

now when the page loads, everything is working fine. but when I update, everything goes bad, it says loginModel is null. the question is, how do I like to track current login users. I have forms authentication enabled. Tnx

error below




 Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 22: Line 23: var loginModel = TempData["loginModel"] as LoginModel; Line 24: string username = loginModel.username; Line 25: string password = loginModel.password; Line 26: premiumModel.username = username; 
+2
asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
Sep 29 '11 at 5:28
source share
4 answers

Confusion

but when I update, everything goes bad, it says loginModel is null

Answer

This is because you read the TempData key, and once it is read, the data will be lost for that particular key.

 var Value = TempData["keyName"] //Once read, data will be lost 



Question

how i like to track current login users

Answer

Thus, to save data even after reading data, you can "Live", as shown below

 var Value = TempData["keyName"]; TempData.Keep(); //Data will not be lost for all Keys TempData.Keep("keyName"); //Data will not be lost for this Key 

TempData also works in new tabs / Windows, for example, the Session variable.

You can also use Session Variable. Only the main problem is that Session Variable compares very strongly with TempData . Finally, you can also store data through controllers / area.

Hope this post helps you.

+6
Jun 19 '13 at 19:28
source share

You only need to save the user ID (username) after user authentication - no password is required. Since such authentication, ASP.NET already supports storing the user ID in the authentication cookie, and you do not need to reinvent the wheel. You can get the identifier using the Controller.User property.

EDIT: I assume that you have correctly configured your forms authentication application. Regardless, here are a few usage guides / tutorials that start you up:

+2
Sep 29 2018-11-11T00:
source share

TempData only works for a single request. Therefore, it is empty when you make the second request. If you want to do it like this, you should use Session instead, or you can see forms authentication .

You should also take VinayC recommendations into account and not store password information in any state, especially in text format.

+1
Sep 29 2018-11-11T00:
source share

I suggest you create a new MVC 3 project in Visual Studio via File> New. He will set up forms authentication for you so that you can see the best practices for the login and registration pages, the user’s signature to / from the session cookie, and the display of user information such as username.

+1
Sep 29 '11 at 6:20
source share



All Articles