Esempio n. 1
0
        protected virtual bool CheckAuth(IFeatureCollection features)
        {
            if (this.GetType().GetCustomAttributes(TokenTableAuthAttributeType, false).FirstOrDefault() == null)
            {
                return(true);
            }

            if (null == features)
            {
                return(false);
            }

            foreach (var kvp in this.Context.Features)
            {
                if (kvp.Value is HttpConnectionContext httpContext)
                {
                    QueryString?qs = httpContext.HttpContext?.Request?.QueryString;
                    if (null != qs)
                    {
                        QueryString queryString = qs.Value;
                        if (queryString.HasValue)
                        {
                            string str = queryString.Value;
                            if (!String.IsNullOrEmpty(str))
                            {
                                if (str[0] == '?')
                                {
                                    str = str.Remove(0, 1);
                                }

                                NameValueCollection query = System.Web.HttpUtility.ParseQueryString(str);
                                string tokenStr           = query["token"];
                                if (Guid.TryParse(tokenStr, out Guid token))
                                {
                                    return(TokenTable.Instance.TryAuthenticateToken(token, out var user));//it does not auth api's. It is ok if user has been loggin.
                                }
                            }
                        }
                    }

                    break;
                }
            }

            return(false);
        }
        private HttpContext CreateContext(string scheme, HostString host, PathString?path = null, QueryString?query = null, string method = null)
        {
            HttpContext context = new DefaultHttpContext();

            context.Request.Scheme = scheme;
            context.Request.Host   = host;

            if (path.HasValue)
            {
                context.Request.Path = path.Value;
            }

            if (query.HasValue)
            {
                context.Request.QueryString = query.Value;
            }

            if (!string.IsNullOrEmpty(method))
            {
                context.Request.Method = method;
            }

            Assert.Null(context.Features.Get <RequestTelemetry>());

            return(context);
        }
        private static Uri CreateUri(string scheme, HostString host, PathString?path = null, QueryString?query = null)
        {
            string uriString = string.Format(CultureInfo.InvariantCulture, "{0}://{1}", scheme, host);

            if (path != null)
            {
                uriString += path.Value;
            }
            if (query != null)
            {
                uriString += query.Value;
            }
            return(new Uri(uriString));
        }