本文介绍了ASP.NET MVC Cookie实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现我的应用程序的基本cookie的帮手。
主要是我的基本控制器每次检查是否如果没有设置cookie。
如果饼干

 公共类的myCookie
{    公共静态字符串CookieName {获取;设置;}
    公共虚拟用户用户{搞定;组; }
    公共虚拟应用应用{搞定;组; }
    公共myCookie时(应用程序)
    {
        CookieName =myCookie时+应用;
        应用=应用;
    }    公共无效的setcookie(用户用户)
    {
        的HttpCookie的myCookie = HttpContext.Current.Request.Cookies [CookieName]?新的HttpCookie(CookieName);
        myCookie.Values​​ [用户ID] = user.UserId.ToString();
        myCookie.Values​​ [LastVisit] = DateTime.Now.ToString();
        myCookie.Expires = DateTime.Now.AddDays(365);
        HttpContext.Current.Response.Cookies.Add(myCookie时);
    }    公众的HttpCookie的getCookie()
    {
        的HttpCookie的myCookie = HttpContext.Current.Request.Cookies [CookieName]
        如果(myCookie时!= NULL)
        {
            INT用户id = Convert.ToInt32(myCookie.Values​​ [用户ID]);
            用户的用户= session.Get<使用者>(用户ID);
            返回用户;
        }
        返回null;
    }
}

如果会话是空我试图从cookie来得到或者会话初始化我设置Cookie,但我从来没有看到我的浏览器cookie。什么是错的?

我总是开始会议,但与用户id = 0
为了获得cookie的cookie,并设置会话:

 如果(用户ID == 0)
{
    myCookie时myCookie时=新的myCookie(_app);
    用户的用户= cookieHelper.GetCookie();
    如果(用户!= NULL)
        SessionHelper.SetSession(用户);
}


解决方案

我的工作实现(基本版)

 公共类CookieHelper
{公共静态字符串CookieName {获取;设置;}
公共虚拟应用应用{搞定;组; }
公共myCookie时(应用程序)
{
    CookieName =myCookie时+应用;
}公共静态无效的setcookie(用户用户,团体与团体,INT cookieExpireDate = 30)
{
    的HttpCookie myCookie时=新的HttpCookie(CookieName);
    myCookie时[用户ID] = user.UserId.ToString();
    myCookie.Expires = DateTime.Now.AddDays(cookieExpireDate);
    HttpContext.Current.Response.Cookies.Add(myCookie时);
 }
 }

如果会话/ cookie是空(实际上是用户id = 0)

 如果(用户ID == 0){
    CookieHelper myCookie时=新的Cookie(_app);
    如果(myCookie时!= NULL)
    {
        用户id = Convert.ToInt32(System.Web.HttpContext.Current.Request.Cookies [myCookie.CookieName] [将把userid]);
        如果(用户ID大于0)
        {
           SessionHelper.SetSession(用户ID);
        }
    }
}

I try to implement a basic cookie helper in my application.Mainly I check in base controller everytime whether or not if cookie is set.If cookie

public class MyCookie
{

    public static string CookieName {get;set;}
    public virtual User User { get; set; }
    public virtual Application App { get; set; }


    public MyCookie(Application app)
    {
        CookieName = "MyCookie" + app;
        App = app;
    }

    public void SetCookie(User user)
    {
        HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName] ?? new HttpCookie(CookieName);
        myCookie.Values["UserId"] = user.UserId.ToString();
        myCookie.Values["LastVisit"] = DateTime.Now.ToString();
        myCookie.Expires = DateTime.Now.AddDays(365);
        HttpContext.Current.Response.Cookies.Add(myCookie);
    }

    public HttpCookie GetCookie()
    {
        HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName];
        if(myCookie != null)
        {
            int userId = Convert.ToInt32(myCookie.Values["UserId"]);
            User user = session.Get<User>(userId);
            return user;
        }
        return null;
    }
}

if session is null I try to get from cookie or if session initialize I set cookie but I never see my cookie in browser. What is wrong?

I always start session but with userId=0To get cookie and set session from cookie:

if (userId == 0)
{
    MyCookie myCookie = new MyCookie(_app);
    User user = cookieHelper.GetCookie();
    if (user != null)
        SessionHelper.SetSession(user);
}
解决方案

My Working Implementation (Basic Version)

public class CookieHelper
{

public static string CookieName {get;set;}
public virtual Application App { get; set; }


public MyCookie(Application app)
{
    CookieName = "MyCookie" + app;
}

public static void SetCookie(User user, Community community, int cookieExpireDate = 30)
{
    HttpCookie myCookie= new HttpCookie(CookieName);
    myCookie["UserId"] = user.UserId.ToString();
    myCookie.Expires = DateTime.Now.AddDays(cookieExpireDate);
    HttpContext.Current.Response.Cookies.Add(myCookie);
 }
 }

if session/cookie is null (actually userid=0)

if (userId == 0){
    CookieHelper myCookie = new Cookie(_app);
    if (myCookie  != null)
    {
        userId = Convert.ToInt32(System.Web.HttpContext.Current.Request.Cookies[myCookie.CookieName]["userId"]);
        if(userId>0)
        {
           SessionHelper.SetSession(userId);
        }
    }
}

这篇关于ASP.NET MVC Cookie实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:22