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); }