示例#1
0
        public static long GetCurrentGuestId(this HttpContextBase ctx)
        {
            bool needRecookie = false;

            long   guestId     = -1;
            string guestValue  = "-1";
            var    cookieValue = ctx.Request.Cookies[guest_cookie_name];

            if (!string.IsNullOrEmpty(cookieValue?.Value))
            {
                if (RegexGuestId.IsMatch(cookieValue.Value) && !ConfigSetting.ProtectGuestId)
                {
                    guestValue = cookieValue.Value;
                    //needRecookie = ProtectGuestId;
                }
                else if (cookieValue.Value.Length > 40)
                {
                    var unprotected = XnAuthentication.UnprotectData(encryptionKey, validationKey, cookieValue.Value);
                    if (unprotected != null && RegexGuestId.IsMatch(unprotected))
                    {
                        guestValue   = unprotected;
                        needRecookie = !ConfigSetting.ProtectGuestId;
                    }
                }
            }

            long.TryParse(guestValue, out guestId);

            if (guestId > -2)
            {
                //userId = GuestHandler.GenerateGuestId();
                var ipByInt = IpExtenstions.IpToInt(ctx.GetClientIP());
                guestValue   = String.Format(guestIdFormatter, ipByInt, GetRnd());
                guestId      = long.Parse(guestValue);
                needRecookie = true;
            }
            if (needRecookie)
            {
                var newCookieValue = ConfigSetting.ProtectGuestId ? XnAuthentication.ProtectData(encryptionKey, validationKey, guestValue) : guestValue;
                var cookie         = new HttpCookie(guest_cookie_name, newCookieValue);
                var requestDomain  = ctx.Request.Url?.Host.Split('.');
                if (requestDomain?.Length > 2)
                {
                    cookie.Domain = requestDomain[requestDomain.Length - 2] + "."
                                    + requestDomain[requestDomain.Length - 1];
                }
                else
                {
#if DEBUG
                    cookie.Domain = "Xn.dev";
#else
                    cookie.Domain = "Xn.cn";
#endif
                }
                cookie.Shareable = false;
                cookie.Expires   = DateTime.Now.AddYears(5);
                ctx.Response.SetCookie(cookie);
            }
            return(guestId);
        }
示例#2
0
        public ResultWithCodeEntity Loging(LoginModel login)
        {
            var token = XnAuthentication.GetValidateCookie();

            if (!xnValidateCodeHandler.IsAuthCode(token, login.Code))
            {
                return(Result.Error(ResultCode.ValidateCodeError));
            }

            var admin = adminRepository.GetInfo(login.UserName, login.Password);

            if (admin == null || admin.Id <= 0)
            {
                return(Result.Error(ResultCode.UserNotExist));
            }
            if (!string.IsNullOrEmpty(admin.MacAddress))
            {
            }
            if (!string.IsNullOrEmpty(admin.IpAddress))
            {
            }
            //将用户在token 写入cookie
            XnAuthentication.SetAuthCookie(admin.Id.ToString());
            return(Result.Success());
        }
        /// <summary>
        /// 检测验证码是否正确
        /// </summary>
        /// <param name="token"></param>
        /// <param name="codeValue"></param>
        /// <returns></returns>
        public bool CheckValidateCode(string codeValue)
        {
            var token = XnAuthentication.GetValidateCookie();

            if (string.IsNullOrEmpty(token))
            {
                return(false);
            }
            return(xnValidateCodeHandler.IsAuthCode(token, codeValue));
        }
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public byte[] GetValidateCode()
        {
            var strCode = CreateValidateCode(CodeLenght);

            if (string.IsNullOrEmpty(strCode))
            {
                return(new byte[0]);
            }
            var imageByte = CreateValidateGraphic(strCode);

            if (imageByte == null || imageByte.Length <= 0)
            {
                return(new byte[0]);
            }
            var token = xnValidateCodeHandler.SetCode(strCode);

            XnAuthentication.SetValidateCookie(token);
            return(imageByte);
        }