簡體   English   中英

Cookie 總是 Null C# ASP.NET MVC

[英]Cookie is always Null C# ASP.NET MVC

我正在使用帶有 OnActionExecuting 事件的 AuthController,該事件確定用戶是否登錄,如果沒有,我將用戶發送到登錄頁面。

public class AuthController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        // load session
        var LoginSession = Session[Constants.USER_SESSION_NAME];

        // load cookie
        HttpCookie LoginCookie = System.Web.HttpContext.Current.Request.Cookies[Constants.USER_COOKIE];

        // create cookie from session 
        if (LoginSession != null && LoginCookie == null)
        {
            var user = (UserLoginDto)LoginSession;

            CreateCookieFromSession(user);
        }

        // create session from cookie
        if (LoginSession == null)
        {
            if (LoginCookie != null)
            {
                if (!string.IsNullOrEmpty(LoginCookie.Value))
                    CreateSessionFromCookie(LoginCookie);
            }               
        }                 

        // if session does not exist send user to login page
        if (Session[Constants.USER_SESSION_NAME] == null)
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                {
                    {"controller", "Login"},
                    {"action", "Index"}
                }
            );
        }    
    }        

    private void CreateSessionFromCookie(HttpCookie cookieObj)
    {            
        UserLoginDto userDto = new UserLoginDto();

        userDto.Id = Convert.ToInt32(cookieObj.Value.Split('&')[0]);                    

        userDto = UserRepository.Get(userDto.Id);

        Session.Add(Constants.USER_SESSION_NAME, userDto);        
    }         

    private HttpCookie CreateCookieFromSession(UserLoginDto user)
    {
        HttpCookie cookie = Request.Cookies[Constants.USER_COOKIE];

        if (cookie == null)
        {
            cookie = new HttpCookie(Constants.USER_COOKIE);
            cookie.Value = user.Id.ToString();
            cookie.Values.Add("Name", Encryptor.encryptString(user.Name));
            cookie.Values.Add("Type", Encryptor.encryptString(user.Type));
            cookie.Values.Add("Token", user.Token);
            cookie.Values.Add("ProfilePictureName", user.ProfilePictureName);
            cookie.Values.Add("ProfilePicturePath", user.ProfilePicturePath);
        }

        cookie.Expires = DateTime.Now.AddYears(1);

        Response.Cookies.Add(cookie);

        return cookie;
    }
}

每隔一個 controller 但登錄擴展 AuthController。

public class HomeController : AuthController
{
    [HttpGet]
    public ActionResult Index()
    {            
        return View();
    }
}

當我嘗試從 OnActionExecuting 方法加載時,cookie 始終為 Null。 誰能發現這個問題? 我也嘗試在 LoginController 中創建 cookie,但仍然是 Null。

希望這將有助於其他開發人員。 終於想通了。 我已經注釋掉了一些值,因為我不需要它們,它們可能使 cookie 值太大。 我相信,所有值加在一起必須超過允許的最大大小,即4096 字節

private HttpCookie CreateCookieFromSession(UserLoginDto user)
{
    HttpCookie cookie = Request.Cookies[Constants.USER_COOKIE];

    if (cookie == null)
    {
        cookie = new HttpCookie(Constants.USER_COOKIE);
        cookie.Value = user.Id.ToString();
        //cookie.Values.Add("Name", Encryptor.encryptString(user.Name));
        //cookie.Values.Add("Type", Encryptor.encryptString(user.Type));
        cookie.Values.Add("Token", user.Token);
        //cookie.Values.Add("ProfilePictureName", user.ProfilePictureName); 
        //cookie.Values.Add("ProfilePicturePath", user.ProfilePicturePath); // base64 string (might have caused the issue)
    }

    cookie.Expires = DateTime.Now.AddYears(1);

    Response.Cookies.Add(cookie);

    return cookie;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM