注销,然后使用mvc 5中的其他用户ID登录

时间:2015-03-21 18:59:39

标签: c# session model-view-controller

如果我退出我的网站并重新登录,则HttpContext.Current.User将保留在之前的登录状态。以下都不起作用:

    //
    // POST: /Account/LogOff
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        var ctx = Request.GetOwinContext();
        var authenticationManager = ctx.Authentication;
        authenticationManager.SignOut();

        System.Web.HttpContext.Current.Application.Remove(System.Web.HttpContext.Current.User.Identity.Name);

        Session.Abandon();
        Session.Clear();

        AuthenticationManager.SignOut();

        FormsAuthentication.SignOut();

        foreach (var cookie in Request.Cookies.AllKeys)
        {
            Request.Cookies.Remove(cookie);
        }
        foreach (var cookie in Response.Cookies.AllKeys)
        {
            Response.Cookies.Remove(cookie);
        }

        FormsAuthentication.SignOut();

        Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddYears(-1);

        HttpContext.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);

        return RedirectToAction("Login", "Account");
    }

干杯

1 个答案:

答案 0 :(得分:0)

我想出来了。有一个系统变量需要重置。我试图做的是有点懒,并得到一个会话变量来获取我的商店程序的数据:

public class MyWhey
{
    public static List<ef_GetMyWhey_Result> GetMyWhey()
    {
        string currentID = HttpContext.Current.GetOwinContext().Authentication.User.Identity.GetUserId();
        return _db.ef_GetMyWhey(currentUserID).ToList();
    }
}

到目前为止,我发现的最好的方法就是从控制器传递它。这是一种痛苦,但它对我非常有效。因此,请从控制器中输入:

using Microsoft.AspNet.Identity;

namespace Whey.Controllers
{
    public class MyWheyController : Controller
    {
        public ActionResult Index()
        {
            WheyPageModel wheyModel = PopulateWheyPageModel.PopulateWheyPage(User.Identity.GetUserId()); // <-- This Fella

            return View(wheyModel);        
        }
    }
}