src/EasyFrameWork/Mvc/ValueProvider/Cookie.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

/* http://www.zkea.net/ 
 * Copyright (c) ZKEASOFT. All rights reserved. 
 * http://www.zkea.net/licenses */

using System;
using System.ComponentModel;
using Microsoft.AspNetCore.Http;

namespace Easy.Mvc.ValueProvider
{
    public class Cookie : ICookie
    {
        private readonly HttpContext _httpContext;
        private const float DefaultExpireDurationMinutes = 43200; // 1 month
        private const bool DefaultHttpOnly = true;
        private const bool ExpireWithBrowser = false;

        public Cookie(IHttpContextAccessor httpContextAccessor)
        {
            this._httpContext = httpContextAccessor.HttpContext;
        }

        public T GetValue<T>(string name)
        {
            return GetValue<T>(name, false);
        }

        public T GetValue<T>(string name, bool expireOnceRead)
        {
            string valuStr = null;
            T value = default(T);

            if (_httpContext.Request.Cookies.TryGetValue(name, out valuStr))
            {
                if (!string.IsNullOrWhiteSpace(valuStr))
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));

                    try
                    {
                        value = (T)converter.ConvertFromString(valuStr);
                    }
                    catch (NotSupportedException)
                    {
                        if (converter.CanConvertFrom(typeof(string)))
                        {
                            value = (T)converter.ConvertFrom(valuStr);
                        }
                    }
                }

                if (expireOnceRead)
                {
                    Delete(name);
                }
            }

            return value;
        }

后面还有 59 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。