Exemplo n.º 1
0
        public async Task Invoke(HttpContext httpContext, IRequestServices rq)
        {
            RequestServices = rq;

            var    client = new ClientInfomation(httpContext);
            string val    = null;
            string refkey = CacheModel.ApiKey + client.GetClientID();
            var    value  = await _cache.GetAsync(refkey);

            if (value != null)
            {
                val = Encoding.UTF8.GetString(value);
            }
            else
            {
                // load form db
                val = CryptographyCore.SHA256_hash(Guid.NewGuid().ToString());
                await _cache.SetAsync(refkey, Encoding.UTF8.GetBytes(val));

                // store to db
            }
            if (RequestServices != null)
            {
                RequestServices.SetClientInfo(new ClientInfo()
                {
                    id           = refkey, key = val, ipAddress = client.GetClientIP(),
                    ConnectionID = client.GetConnectionID(), UserAgent = client.GetUserAgent()
                });
            }
            httpContext.Response.Headers.Append("ClientKey", val);
            if (_next != null && httpContext != null)
            {
                await _next.Invoke(httpContext);
            }
        }
Exemplo n.º 2
0
        public async Task SetKey(HttpContext _httpContext = null)
        {
            HttpContext httpContext = _httpContextAccessor.HttpContext;

            if (_httpContext != null)
            {
                httpContext = _httpContext;
            }
            var    client = new ClientInfomation(httpContext);
            string val    = null;
            string refkey = CacheModel.ApiKey + client.GetClientID();
            var    value  = await _cache.GetAsync(refkey);

            if (value != null)
            {
                val = Encoding.UTF8.GetString(value);
            }
            else
            {
                val = RNG.UniqueString();
                await _cache.SetAsync(refkey, Encoding.UTF8.GetBytes(val));
            }
            if (_RequestServices != null)
            {
                _RequestServices.SetClientInfo(new ClientInfo()
                {
                    id           = refkey,
                    key          = val,
                    ipAddress    = client.GetClientIP(),
                    ConnectionID = client.GetConnectionID(),
                    UserAgent    = client.GetUserAgent()
                });
            }
            httpContext.Response.Headers.Append("clientKey", val);
        }
Exemplo n.º 3
0
 private static void ShowMessage(ClientInfomation data)
 {
     Console.WriteLine($@"               MacAddress:{data.MacAddress ?? string.Empty}");
     Console.WriteLine($@"               ServerAddress:{data.ServerAddress?.ToString()}");
     Console.WriteLine($@"               TransactionID:{data.TransactionID}");
     Console.WriteLine($@"               ClientAddress:{data.ClientAddress?.ToString()}");
 }
Exemplo n.º 4
0
        public async Task SetKey()
        {
            var    client = new ClientInfomation(_httpContext);
            string val    = null;
            string refkey = CacheModel.ApiKey + client.GetClientID();
            var    value  = await _cache.GetAsync(refkey);

            if (value != null)
            {
                val = Encoding.UTF8.GetString(value);
            }
            else
            {
                val = CryptographyCore.SHA256_hash(Guid.NewGuid().ToString());
                await _cache.SetAsync(refkey, Encoding.UTF8.GetBytes(val));
            }
            if (_RequestServices != null)
            {
                _RequestServices.SetClientInfo(new ClientInfo()
                {
                    id           = refkey,
                    key          = val,
                    ipAddress    = client.GetClientIP(),
                    ConnectionID = client.GetConnectionID(),
                    UserAgent    = client.GetUserAgent()
                });
            }
            _httpContext.Response.Headers.Append("clientKey", val);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Check(string key)
        {
            var result = new ApiResultModel <bool>();

            _client = new ClientInfomation(HttpContext);
            var keystore = await _cache.GetAsync(CacheModel.ApiKey + _client.GetClientID());

            if (keystore != null)
            {
                if (Encoding.UTF8.GetString(keystore).Equals(key))
                {
                    result.Data = true;
                    return(Ok(result));
                }
                else
                {
                    result.Notfound();
                    result.Data = false;
                    return(NotFound(result));
                }
            }
            else
            {
                result.Notfound("API key not found");
                return(NotFound(result));
            }
        }
Exemplo n.º 6
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!context.HttpContext.Request.Headers.TryGetValue(ApiKeyHeaderName, out var potentialApiKey))
            {
                //context.Result = new UnauthorizedResult();
                //return;
                throw new UnauthorizedAccessException("Key: is required");
            }
            var    client = new ClientInfomation(context.HttpContext);
            string apiKey = null;
            var    cache  = context.HttpContext.RequestServices.GetRequiredService <IDistributedCache>();
            var    value  = await cache.GetAsync(CacheModel.ApiKey + client.GetClientID());

            if (value != null)
            {
                apiKey = Encoding.UTF8.GetString(value);
            }

            if (apiKey == null || !apiKey.Equals(potentialApiKey))
            {
                //context.Result = new UnauthorizedResult();
                //return;
                throw new UnauthorizedAccessException("Key is invalid, please reopen your appication");
            }
            await next();
        }
Exemplo n.º 7
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var cache  = context.HttpContext.RequestServices.GetRequiredService <IDistributedCache>();
            var result = new ApiResultModel <bool>();

            if (isCheckCleint)
            {
                if (!context.HttpContext.Request.Headers.TryGetValue(ClientKeyHeaderName, out var potentialClientKey))
                {
                    result = new ApiResultModel <bool>();
                    result.Unauthorized("Client Key: is required");
                    context.Result = new JsonResult(result);
                    context.HttpContext.Response.StatusCode = result.Status;
                    return;
                }
                var    client    = new ClientInfomation(context.HttpContext);
                string clientKey = null;
                var    value     = await cache.GetAsync(CacheModel.ApiKey + client.GetClientID());

                if (value != null)
                {
                    clientKey = Encoding.UTF8.GetString(value);
                }

                if (clientKey == null || !clientKey.Equals(potentialClientKey))
                {
                    result = new ApiResultModel <bool>();
                    result.Unauthorized("Client Key is invalid, please reopen your appication");
                    context.Result = new JsonResult(result);
                    context.HttpContext.Response.StatusCode = result.Status;
                    return;
                }
            }
            else if (!context.HttpContext.Request.Headers.TryGetValue(ClientApiHeaderName, out var potentialClientApi))
            {
                result = new ApiResultModel <bool>();
                result.Unauthorized("API Key: is required");
                context.Result = new JsonResult(result);
                context.HttpContext.Response.StatusCode = result.Status;
                return;
            }
            await next();
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Index()
        {
            _httpContext = HttpContext;
            _client      = new ClientInfomation(_httpContext);
            client_id    = _client.GetClientID();
            var result   = new ApiResultModel <ApiClientInfo>();
            var keystore = await _cache.GetAsync(CacheModel.ApiKey + client_id);

            if (keystore != null)
            {
                return(GetKey(keystore));
            }
            else
            {
                await new ClientKey(_cache, _httpContext, _request).SetKey();
                keystore = await _cache.GetAsync(CacheModel.ApiKey + client_id);

                return(GetKey(keystore));
            }
        }