如何在同一解决方案中的项目之间保持HttpContext.Current?

时间:2014-04-28 09:30:24

标签: asp.net-mvc-4 httpcontext formsauthentication

我对FormsAuthentication的概念不熟悉并且正在努力解决问题。

我有一个MVC4项目MyApp.Web,它有一个带有Login方法的AccountController:

public ActionResult Login(LoginModel model, string returnUrl)
{
    string siteName = ConfigurationManager.AppSettings["siteName"];

    if (ModelState.IsValid && MembershipService.Login(model.UserName, model.Password, siteName))
    {
        return RedirectToAction("Main", "Home");
    }
}

MembershipService位于另一个项目(类库)

public bool Login(string username, string password, string site)
{

    // ....
    // do the validation, set up my ClaimsPrincipal and create a FormsAuthenticationTicket,
    // which is stored in the Cookies collection

    // now, we can save the ClaimsPrincipal to the relevant place
    if (HttpContext.Current != null && HttpContext.Current.User != null)
        HttpContext.Current.User = claimsPrincipal;
    else if (Thread.CurrentPrincipal != null)
        Thread.CurrentPrincipal = claimsPrincipal;   

    return success;
}

这一切看起来都很完美,我在必要时进行了验证,创建并存储了ClaimsPrincipal。

我的问题是,当我被重定向时,回到MyApp.Web,回到HomeController,我在Main方法中放了一个断点,HttpContext.Current与我在Membership项目中的断点不一样;我在Login方法中设置的值都没有保留。

那么,简单地说,我如何确保它们是相同的,并且在一个项目中HttpContext.Current上的设置值会继续执行我在同一解决方案中可能拥有的任何其他项目?

由于

0 个答案:

没有答案
相关问题