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); } }
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); }
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); }
private IActionResult GetKey(byte[] keystore) { var result = new ApiResultModel <ApiClientInfo>(); if (keystore != null) { result.Data = new ApiClientInfo() { ip = _client.GetClientIP(), ua = _client.GetUserAgent(), client_id = client_id, key = Encoding.UTF8.GetString(keystore) }; return(Ok(result)); } else { result.BadRequest("Cannot generate API key"); return(BadRequest(result)); } }