Exemple #1
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            //// 檢查request裡是否有「Authorization」這個Header
            var  token         = Enumerable.Empty <string>();
            bool isHeaderExist = request.Headers.TryGetValues(_header, out token);

            if (isHeaderExist)
            {
                if (token.Count() > 0)
                {
                    authFunc auth   = new authFunc();
                    string   userid = auth.validatesToken(token.First());
                    this.SetPrincipal(userid);
                }
            }
            return(base.SendAsync(request, cancellationToken));
        }
Exemple #2
0
        protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            //由token換成userid

            string userid        = "null";
            var    token         = Enumerable.Empty <string>();
            bool   isHeaderExist = request.Headers.TryGetValues(_header, out token);

            if (isHeaderExist)
            {
                if (token.Count() > 0)
                {
                    authFunc auth = new authFunc();
                    userid = auth.validatesToken(token.First());
                }
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var info = new RequestLogInfo
            {
                HttpMethod  = request.Method.Method,
                UrlAccessed = request.RequestUri.AbsoluteUri,
                IpAddress   = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0",
                RequestTime = DateTime.Now,
                Token       = this.GetToken(request),
                Signature   = userid,
                Timestamp   = this.GetTimestamp(request),
                BodyContent = request.Content == null ? string.Empty : request.Content.ReadAsStringAsync().Result
            };

            var logContent = this._serializer.Serialize <RequestLogInfo>(info);

            this._log.Save(logContent);

            return(request);
        }