1

我正在使用“开箱即用”MVC3 安全框架,我的应用程序在从 Visual Studio 2010 启动时运行良好,但是当我将它部署到服务器时,当 HomeController 引用它时,我存储在 Session 中的用户对象为空.

非常感谢任何帮助。

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
        try
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    this.log.Warn("Calling SetUserSession");
                    // set user session
                    SetUserSession(model.UserName);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {

                        return RedirectToAction("Index", "Home", new { area = "" });
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
        }
        catch (Exception err)
        {
            this.log.Error(err);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
}



public void SetUserSession(string userName)
{            

        int userId = userDAO.GetUserId(userName);
        FNHSessionManager sessionManager = new FNHSessionManager(connstr.ConnectionString, FNHSessionManager.DatabaseType.MsSql);
        FNHRepository<User> userRepos = new FNHRepository<User>(sessionManager);

        System.Web.HttpContext.Current.Session["fnhSession"] = sessionManager;

        User tmpUser = (User)userRepos.RetrieveById(userId);

        Product prod = productDAO.Select(tmpUser.DefaultProduct.Id);

        System.Web.HttpContext.Current.Session["currentUser"] = tmpUser;
        System.Web.HttpContext.Current.Session["currentUserName"] = userName;
        System.Web.HttpContext.Current.Session["currentTitleName"] = prod.Name;
}



[HandleError]
public class HomeController : Controller
{
    private readonly IProductDAO _productDAO;
    private readonly User currentUser;        
    private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);



    public HomeController(IProductDAO productDAO)
    {
        _productDAO = productDAO;
        this.currentUser = (User)(System.Web.HttpContext.Current.Session["currentUser"]);            
    }


    public ActionResult Index()
    {            
        if (currentUser == null) // current user is null here when deployed to server
        {                
            return RedirectToAction("LogOn", "Account");                
        }
        else
        {
            var viewModel = PopulateHomeViewModel();
            return View("Home", viewModel);
        }
    }
}
4

0 回答 0