コード例 #1
0
        /// <summary>
        /// 运行监视
        /// </summary>
        /// <param name="action">监视回调</param>
        public void Run(Action <string> action)
        {
            IPAddress   localAddr = IPAddress.Parse(GetLocalIp());
            TcpListener server    = new TcpListener(localAddr, port);

            try
            {
                server.Start();
                Byte[] bytes = new Byte[buffer];
                String data  = null;
                while (true)
                {
                    TcpClient client = server.AcceptTcpClient();
                    data = null;
                    NetworkStream stream = client.GetStream();
                    int           i;
                    try
                    {
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            action(data);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log4Net.ErrorLog(ex);
                    }
                    client.Close();
                }
            }
            catch (SocketException ex)
            {
                Log4Net.ErrorLog(ex);
            }
            finally
            {
                server.Stop();
            }
        }
コード例 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="filterContext"></param>
 public override void OnException(ExceptionContext filterContext)
 {
     filterContext.ExceptionHandled = true;
     Log4Net.ErrorLog(filterContext.Exception);
     filterContext.Result = new ResponseModel <string>(ErrorCode.server_exception, filterContext.Exception.Message);
 }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnAuthorization(AuthorizationFilterContext filterContext)
        {
            var actionDescriptor = (ControllerActionDescriptor)filterContext.ActionDescriptor;
            IEnumerable <CustomAttributeData> methodAttributes     = actionDescriptor.MethodInfo.CustomAttributes;
            IEnumerable <CustomAttributeData> controllerAttributes = actionDescriptor.ControllerTypeInfo.CustomAttributes;
            bool   isAuthorization = true;
            string permissionName  = "";

            foreach (CustomAttributeData item in controllerAttributes)
            {
                if (item.AttributeType.Name == "AllowAnonymousAttribute")
                {
                    isAuthorization = false;
                }
                if (item.AttributeType.Name == "SSOAuthorizeAttribute")
                {
                    isAuthorization = true;
                    if (item.ConstructorArguments.Count > 0)
                    {
                        permissionName = item.ConstructorArguments[0].Value.ToString();
                    }
                }
            }
            foreach (CustomAttributeData item in methodAttributes)
            {
                if (item.AttributeType.Name == "AllowAnonymousAttribute")
                {
                    isAuthorization = false;
                }
                if (item.AttributeType.Name == "SSOAuthorizeAttribute")
                {
                    isAuthorization = true;
                    if (item.ConstructorArguments.Count > 0)
                    {
                        permissionName = item.ConstructorArguments[0].Value.ToString();
                    }
                }
            }
            if (!isAuthorization)
            {
                return;
            }
            //验证配置文件
            if (!VerifyConfig(filterContext))
            {
                return;
            }
            HttpRequest request     = filterContext.HttpContext.Request;
            var         ssourl      = request.Query["ssourls"];
            var         absoluteUrl = AppSettings.GetAbsoluteUri(request);

            if (!string.IsNullOrEmpty(ssourl)) //sso 退出
            {
                var returnUrl = request.Query["returnUrl"];
                ////////清除本站cookie
                List <string> ssoUrls = JsonSerializerHelper.Deserialize <List <string> >(Encoding.UTF8.GetString(Convert.FromBase64String(Base64SecureURL.Decode(ssourl))));
                var           cookie  = request.Cookies[CookieKey];
                if (cookie != null)
                {
                    filterContext.HttpContext.Response.Cookies.Delete(CookieKey);
                }
                /////////////////////
                for (var i = 0; i < ssoUrls.Count; i++)
                {
                    if (absoluteUrl.Contains(ssoUrls[i]))
                    {
                        ssoUrls.RemoveAt(i);
                        break;
                    }
                }
                if (ssoUrls.Count > 0)
                {
                    string newSsoUrls = JsonSerializerHelper.Serialize(ssoUrls);
                    filterContext.Result = new RedirectResult(ssoUrls[0] + "?ssourls=" + newSsoUrls.StrToBase64() + "&returnUrl=" + returnUrl);
                }
                else //最后一个
                {
                    filterContext.Result = new RedirectResult(BaseUrl + "?returnUrl=" + returnUrl);
                }
                return;
            }
            string authorization = JwtManager.GetAuthorization(request, CookieKey);
            string ticket        = request.Query["ticket"];

            if (string.IsNullOrEmpty(authorization))
            {
                if (string.IsNullOrEmpty(ticket))
                {
                    filterContext.Result = GetActionResult(absoluteUrl);
                    return;
                }
                else
                {
                    string from = AppSettings.GetApplicationUrl(request).ReplaceHttpPrefix().TrimEnd('/');
                    authorization = GetTokenByTicket(from, ticket, request.HttpContext.Connection.RemoteIpAddress.ToString());
                    if (!string.IsNullOrEmpty(authorization))
                    {
                        if (CookieTime != "session")
                        {
                            filterContext.HttpContext.Response.Cookies.Append(CookieKey, authorization, new CookieOptions()
                            {
                                Expires = DateTime.Now.AddMinutes(Convert.ToInt32(CookieTime))
                            });
                        }
                        else
                        {
                            filterContext.HttpContext.Response.Cookies.Append(CookieKey, authorization);
                        }
                    }
                    else
                    {
                        filterContext.Result = GetActionResult(absoluteUrl);
                        return;
                    }
                }
            }
            try
            {
                var principal = JwtManager.ParseAuthorization(authorization, SecretKey, filterContext.HttpContext);
                filterContext.HttpContext.User = principal;
                if (!CheckPermission(permissionName, authorization))
                {
                    filterContext.Result = new ResponseModel <string>(ErrorCode.error_permission, "");
                }
            }
            catch (Exception ex) //token失效
            {
                Log4Net.ErrorLog(ex);
                var httpCookie = filterContext.HttpContext.Request.Cookies[CookieKey];
                if (httpCookie != null)
                {
                    filterContext.HttpContext.Response.Cookies.Delete(CookieKey);
                }
                filterContext.Result = GetActionResult(absoluteUrl);
            }
        }
コード例 #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 public void OnException(ExceptionContext context)
 {
     context.ExceptionHandled = true;
     Log4Net.ErrorLog(context.Exception);
     context.Result = new ResponseModel <string>(ErrorCode.server_exception, context.Exception.Message);
 }