Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Redis!");

            _redisHandler = new RedisHandler("127.0.0.1:6379");

            Console.WriteLine($"connected?{_redisHandler.IsConnected}");

            Console.WriteLine($"set?{_redisHandler.Set("key1", "value1")}");

            var getValue = _redisHandler.Get <string>("key1");

            Console.WriteLine($"get key1?{getValue}");

            Console.WriteLine($"remove key1?{_redisHandler.Remove("key1")}");

            getValue = _redisHandler.Get <string>("key1");
            Console.WriteLine($"get key1?{getValue}");

            dynamic dobj = new ExpandoObject();

            dobj.key1 = "value1";
            dobj.key2 = "value2";
            dobj.key3 = 1000;
            var jsonObject = JsonConvert.SerializeObject(dobj);

            _redisHandler.Set <string>("json", jsonObject);
            Console.WriteLine(_redisHandler.Get <string>("json"));

            Console.WriteLine($"keys:{_redisHandler.GetKeyAll().ToString<String>()}");

            using (var manager = _redisHandler.GetRedisManagerPool())
            {
                using (var client = manager.GetClient())
                {
                    Console.WriteLine($"ping:{client.Ping()}");
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login(UserQuery query)
        {
            try
            {
                if (query == null)
                {
                    _result.status     = (int)HttpStatusCode.BadRequest;
                    _result.msg        = "query is not null";
                    _result.resultData = null;
                    return(BadRequest(_result));
                }
                if (string.IsNullOrWhiteSpace(query.CName))
                {
                    _result.status     = (int)HttpStatusCode.BadRequest;
                    _result.msg        = "CName is not null";
                    _result.resultData = null;
                    return(BadRequest(_result));
                }
                if (string.IsNullOrWhiteSpace(query.CPasswd))
                {
                    _result.status     = (int)HttpStatusCode.BadRequest;
                    _result.msg        = "CPasswd is not null";
                    _result.resultData = null;
                    return(BadRequest(_result));
                }
                var res = await _customerServer.UserLogin(new UserQuery()
                {
                    CName   = query.CName,
                    CPasswd = query.CPasswd
                });

                if (res == null)
                {
                    _result.status     = (int)HttpStatusCode.Forbidden;
                    _result.msg        = "fail";
                    _result.resultData = res;
                    return(NotFound(_result));
                }
                var tokenHandler    = new JwtSecurityTokenHandler();
                var key             = Encoding.UTF8.GetBytes(_configuration.GetSection("clientJwt")?.Value);
                var authTime        = DateTime.UtcNow;
                var expiresAt       = authTime.AddSeconds(Convert.ToDouble(_configuration.GetSection("loginTimeSpan")?.Value));
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(JwtClaimTypes.Audience, "api"),
                        new Claim(JwtClaimTypes.Id, res?.CId.ToString()),
                        new Claim(JwtClaimTypes.Name, res?.CName)
                    }),
                    Expires            = expiresAt,
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };
                var token       = tokenHandler.CreateToken(tokenDescriptor);
                var tokenString = tokenHandler.WriteToken(token);
                //token
                HttpContext.Response.Cookies.Append(SystemCodeEnum.P00002.ToString(), tokenString, new CookieOptions
                {
                    HttpOnly = true
                });
                HttpContext.Response.Cookies.Append(SystemCodeEnum.uid.ToString(), res?.CId.Value.ToString("N"), new CookieOptions
                {
                    HttpOnly = false
                });
                //存入redis。操作需要登陆的界面时,需要用token+userid验证
                var ret = _redis.Set(res?.CId.Value.ToString("N"), tokenString, TimeSpan.FromSeconds(Convert.ToDouble(_configuration.GetSection("loginTimeSpan")?.Value)));
                _result.status     = (int)HttpStatusCode.OK;
                _result.msg        = "success";
                _result.resultData = res;
                return(Ok(_result));
            }
            catch (Exception ex)
            {
                _log.Error("UserLogin method error:" + ex);
                _result.status     = (int)HttpStatusCode.InternalServerError;
                _result.msg        = "fail";
                _result.resultData = null;
                return(StatusCode(_result.status, _result));
            }
        }