我是mvc的新手。我的问题是:在用户logsIn
输入用户凭据后,调用HttpPost
控制器并且该控制器调用模型类内的登录方法,在该类中创建一个存储用户详细信息的会话
现在问题开始了,我可以访问HttpPost
登录控制器中的会话,但不能访问Home控制器内的会话。
控制器代码:
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
}
}
型号代码:
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;
}
}
}
主页视图:
@{
ViewBag.Title = "Home";
Layout = "~/Views/Shared/TestMaster.cshtml";
}
@{
var sessionVar = Session["UserDetail"].ToString();
<span>@sessionVar</span>
}
</div>
家庭控制器代码:
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();
}
}
}
现在在Session["UserDetail"].ToString()
中它表示对象引用错误!