I am new to mvc. My question is: After entering the user credentials when user logsIn
then a HttpPost
controller is called and that controller calls a method for login inside the model class, inside that class a session is created which stores user details.
Now the problem starts, i can access the session in HttpPost
login controller but not inside the Home controller.
Controller Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test.Models;
using System.Web.Security;
using System.Configuration;
using System.Xml;
namespace Test.Controllers
{
public class LoginController : Controller
{
#region LogIn
//
// GET: /Login/
[HttpGet]
public ActionResult Index()
{
}
[HttpPost]
public ActionResult Index(LogInModel model, string Command)
{
//Check the button clicked
switch (Command)
{
case "Log In":
if (ModelState.IsValid)
{
string sUser = model.CheckValidity();
if (sUser != "")
{
//Check session value
string strCheckSession = Session["UserDetail"].ToString();
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Invalid UserName / Password!");
return View(model);
}
}
break;
case "Forgot Password":
return RedirectToAction("Index", "ForgotPassword");
break;
default:
break;
}
// If we got this far, something failed, redisplay form
return View(model);
}
#endregion
}
}
Model Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Globalization;
using System.Web.Security;
//using Helpers;
using System.Web.Mvc;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;
namespace Test.Models
{
public class LogInModel //: Page
{
[Required(ErrorMessage = "User name required.")]
[DisplayName("User name")]
public string UserName { get; set; }
[Required(ErrorMessage = "Password required.")]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
[DisplayName("Remember me?")]
public bool RememberMe { get; set; }
/// <summary>
/// Checks if user with given password exists in the database
/// </summary>
/// <param name="_username">User name</param>
/// <param name="_password">User password</param>
/// <returns>True if user exist and password is correct</returns>
public string CheckValidity()
{
try{
XmlDocument objIxml = new XmlDocument();
//add input details in objIxml
XmlDocument objOxml = new XmlDocument();
//The Authentication method will connect to database and return the output as xml
objOxml = Authentication(objIxml);
HttpContext.Current.Session["UserDetail"] = objOxml.OuterXml;
if(objOxml.OuterXml != "")
return "Valid user";
else
return "";
}
catch (Exception ex)
{
//throw Exception;
}
}
}
Home View:
@{
ViewBag.Title = "Home";
Layout = "~/Views/Shared/TestMaster.cshtml";
}
@{
var sessionVar = Session["UserDetail"].ToString();
<span>@sessionVar</span>
}
</div>
Home Controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test.Models;
namespace Test.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
//object reference Error in session
string str = Session["UserDetail"].ToString();
return View();
}
}
}
Now in Session["UserDetail"].ToString()
it says object reference error !