コード例 #1
0
        /// <summary>
        /// 请求发送前事件
        /// </summary>
        /// <param name="request">请求消息</param>
        /// <param name="channel">信道</param>
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            if (OperationContext.Current != null)
            {
                //WCF客户端获取公钥处理
                MessageHeaders incomingHeaders = OperationContext.Current.IncomingMessageHeaders;

                #region # 验证消息头

                if (!incomingHeaders.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId))
                {
                    string message = "身份认证消息头不存在,请检查程序!";
                    NoPermissionException innerException = new NoPermissionException(message);
                    FaultReason           faultReason    = new FaultReason(message);
                    FaultCode             faultCode      = new FaultCode(HttpStatusCode.Unauthorized.ToString());
                    throw new FaultException <NoPermissionException>(innerException, faultReason, faultCode);
                }

                #endregion

                //读取消息头中的公钥
                Guid publicKey = incomingHeaders.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId);

                //添加消息头
                System.ServiceModel.Channels.MessageHeader outgoingheader = System.ServiceModel.Channels.MessageHeader.CreateHeader(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId, publicKey);
                request.Headers.Add(outgoingheader);
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// 接收请求后事件
        /// </summary>
        /// <param name="request">请求消息</param>
        /// <param name="channel">信道</param>
        /// <param name="instanceContext">WCF实例上下文</param>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            //获取消息头
            MessageHeaders headers = request.Headers;
            string         action  = headers.Action;

            EndpointDispatcher endpointDispatcher  = OperationContext.Current.EndpointDispatcher;
            DispatchOperation  operationDispatcher = endpointDispatcher.DispatchRuntime.Operations.Single(x => x.Action == action);

            /*
             * 通过OperationBehavior设置Impersonation属性,
             * 默认值为ImpersonationOption.NotAllowed,
             * 当ImpersonationOption.NotAllowed时验证身份,
             * 如无需验证身份,则将Impersonation属性赋值为ImpersonationOption.Allowed。
             */
            if (operationDispatcher.Impersonation == ImpersonationOption.NotAllowed)
            {
                #region # 验证消息头

                if (!headers.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId))
                {
                    string message = "身份认证消息头不存在,请检查程序!";
                    NoPermissionException innerException = new NoPermissionException(message);
                    FaultReason           faultReason    = new FaultReason(message);
                    FaultCode             faultCode      = new FaultCode(HttpStatusCode.Unauthorized.ToString());
                    throw new FaultException <NoPermissionException>(innerException, faultReason, faultCode);
                }

                #endregion

                //读取消息头中的公钥
                Guid publicKey = headers.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId);

                //认证
                lock (_Sync)
                {
                    //以公钥为键,查询分布式缓存,如果有值则通过,无值则不通过
                    LoginInfo loginInfo = CacheMediator.Get <LoginInfo>(publicKey.ToString());
                    if (loginInfo == null)
                    {
                        string message = "身份过期,请重新登录!";
                        NoPermissionException innerException = new NoPermissionException(message);
                        FaultReason           faultReason    = new FaultReason(message);
                        FaultCode             faultCode      = new FaultCode(HttpStatusCode.Unauthorized.ToString());
                        throw new FaultException <NoPermissionException>(innerException, faultReason, faultCode);
                    }

                    //通过后,重新设置缓存过期时间
                    CacheMediator.Set(publicKey.ToString(), loginInfo, DateTime.Now.AddMinutes(GlobalSetting.AuthenticationTimeout));
                }
            }

            return(null);
        }
コード例 #3
0
        /// <summary>
        /// 获取商品集
        /// </summary>
        /// <returns>商品集</returns>
        public string GetProducts()
        {
            Console.WriteLine("Hello World");

            string headerName = "headerName";
            string headerNs   = "headerNs";

            MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;

            if (headers.Any(x => x.Name == headerName && x.Namespace == headerNs))
            {
                string header = headers.GetHeader <string>(headerName, headerNs);
                Console.WriteLine($"消息头:\"{header}\"");
            }

            return("Hello World");
        }
コード例 #4
0
        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <returns>登录信息</returns>
        public LoginInfo GetLoginInfo()
        {
            if (OperationContext.Current != null)
            {
                //获取消息头
                MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
                if (!headers.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId))
                {
                    return(null);
                }

                Guid      publicKey = headers.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId);
                LoginInfo loginInfo = CacheMediator.Get <LoginInfo>(publicKey.ToString());

                return(loginInfo);
            }

            return(null);
        }
コード例 #5
0
        /// <summary>
        /// 获取消息头
        /// </summary>
        /// <returns>消息头</returns>
        /// <remarks>此方法用于测试,将客户端发送的消息头返回给客户端</remarks>
        public string GetHeader()
        {
            //获取消息头
            MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;

            #region # 验证消息头

            if (!headers.Any(x => x.Name == CommonConstants.WCFAuthenticationHeader && x.Namespace == GlobalSetting.ApplicationId))
            {
                throw new NullReferenceException("身份认证消息头不存在,请检查程序!");
            }

            #endregion

            //读取消息头中的公钥
            Guid publishKey = headers.GetHeader <Guid>(CommonConstants.WCFAuthenticationHeader, GlobalSetting.ApplicationId);

            return(publishKey.ToString());
        }
コード例 #6
0
        /// <summary>
        /// 接收请求后事件
        /// </summary>
        /// <param name="request">请求消息</param>
        /// <param name="channel">信道</param>
        /// <param name="instanceContext">WCF实例上下文</param>
        /// <returns></returns>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            //如果是身份认证接口,无需认证
            if (OperationContext.Current.EndpointDispatcher.ContractName != "IAuthenticationContract")
            {
                //获取消息头
                MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;

                #region # 验证消息头

                if (!headers.Any(x => x.Name == CommonConstants.WcfAuthHeaderName && x.Namespace == CommonConstants.WcfAuthHeaderNamespace))
                {
                    throw new NullReferenceException("身份认证消息头不存在,请检查程序!");
                }

                #endregion

                //读取消息头中的公钥
                Guid publicKey = headers.GetHeader <Guid>(CommonConstants.WcfAuthHeaderName, CommonConstants.WcfAuthHeaderNamespace);

                //认证
                lock (_Sync)
                {
                    //以公钥为键,查询分布式缓存,如果有值则通过,无值则不通过
                    LoginInfo loginInfo = CacheMediator.Get <LoginInfo>(publicKey.ToString());

                    if (loginInfo == null)
                    {
                        throw new NoPermissionException("身份过期,请重新登录!");
                    }

                    //通过后,重新设置缓存过期时间
                    CacheMediator.Set(publicKey.ToString(), loginInfo, DateTime.Now.AddMinutes(_Timeout));
                }
            }

            return(null);
        }
コード例 #7
0
        /// <summary>
        /// 请求发送前事件
        /// </summary>
        /// <param name="request">请求消息</param>
        /// <param name="channel">信道</param>
        /// <returns></returns>
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            //WCF客户端获取公钥处理
            MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;

            #region # 验证消息头

            if (!headers.Any(x => x.Name == CommonConstants.WcfAuthHeaderName && x.Namespace == CommonConstants.WcfAuthHeaderNamespace))
            {
                throw new NullReferenceException("身份认证消息头不存在,请检查程序!");
            }

            #endregion

            //读取消息头中的公钥
            Guid publishKey = headers.GetHeader <Guid>(CommonConstants.WcfAuthHeaderName, CommonConstants.WcfAuthHeaderNamespace);

            //添加消息头
            MessageHeader header = MessageHeader.CreateHeader(CommonConstants.WcfAuthHeaderName, CommonConstants.WcfAuthHeaderNamespace, publishKey);
            request.Headers.Add(header);

            return(null);
        }