SiteHelper.cs
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Web;
namespace Quiz.SiteBase
{
    public class SiteHelper
    {
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static string UserId
        {
            get
            {
                try
                {
                    return Utility.Helper.DESDeCode(UserCookie[SiteContant.CookieKey.UserCode], SiteContant.CookieKey.UserKey);
                }
                catch (Exception)
                {
                    return "";
                }
            }
            set
            {
                SetUidCookie(value);
            }
        }
        public static string CurrentUrl
        {
            get { return string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.RawUrl); }
        }
        #region UserCookie
        ///// <summary>
        ///// Cookie域
        ///// </summary>
        //public static string CookieDomain
        //{
        //    get
        //    {
        //        var domain = HttpContext.Current.Request.Url.Host.StartsWith("60.190") ? "60.190.202.41" : Common.Config.RootURL;
        //        return IsLocal ? null : domain;
        //    }
        //}
        /// <summary>
        /// 客户端操作Cookie
        /// </summary>
        public static HttpCookie UserCookie
        {
            get
            {
                HttpCookie httpcookie = SiteCookie.GetCookies(SiteContant.CookieKey.UserInfo);
                if (httpcookie == null)
                {
                    httpcookie = new HttpCookie(SiteContant.CookieKey.UserInfo);
                    //httpcookie.Domain = CookieDomain;
                    SiteCookie.SaveCookie(httpcookie, 3600);
                }
                //else httpcookie.Domain = CookieDomain;
                return httpcookie;
            }
        }
        //设置用户的Cookie
        public static void SetUidCookie(string value)
        {
            string key = string.IsNullOrEmpty(value) ? value : Utility.Helper.DESEnCode(value, SiteContant.CookieKey.UserKey);
            UserCookie.Values[SiteContant.CookieKey.UserCode] = key;
            SiteCookie.SaveCookie(UserCookie, 3600);
        }
        //设置用户的Cookie
        public static void SetUidCookie(string key, string value)
        {
            UserCookie.Values[key] = value;
            SiteCookie.SaveCookie(UserCookie, 3600);
        }
        #endregion
    }
}