/// <summary> /// Sends request to GIPHY and returns it in JSON form /// </summary> /// <param name="word">Word to look up</param> /// <returns>Returns GIPHY response in JSON form</returns> public JsonResult TranslateGIF(string word) { //URI to contact GIPHY's servers string uri = "https://api.giphy.com/v1/stickers/translate?api_key=" + System.Web.Configuration.WebConfigurationManager.AppSettings["GiphyKey"] + "&s=" + word; //Create a web request WebRequest dataRequest = WebRequest.Create(uri); //Get the (JSON) data Stream dataStream = dataRequest.GetResponse().GetResponseStream(); //Parse the received (JSON) data var parsedData = new System.Web.Script.Serialization.JavaScriptSerializer() .DeserializeObject(new StreamReader(dataStream) .ReadToEnd()); //*****Requests logging code***** AccessLogs log = new AccessLogs(); log.IPAddress = Request.UserHostAddress; //logs client's IP address log.KeyWord = word; //logs client's requested word log.AgentString = Request.UserAgent; //logs client's browser information //saves current request to database db.Logs.Add(log); db.SaveChanges(); //******************************* //return the (JSON) data return(Json(parsedData, JsonRequestBehavior.AllowGet)); }
public void Configuration(IAppBuilder app) { bool debugmode = false; app.Use(async(environment, next) => { AccessLogs log = null; ApplicationDbContext _context = new ApplicationDbContext(); var req = environment.Request; string endpoint = environment.Request.Path.ToString(); Uri uri = req.Uri; var seg = uri.Segments; string verb = environment.Request.Method; int userid = 0; Int32.TryParse(environment.Request.Headers.Get("id"), out userid); string token = environment.Request.Headers.Get("token"); ValidateAuth validator = new ValidateAuth(); int resourceid = 0; //tiene resourseid if (Int32.TryParse(seg[seg.Length - 1], out resourceid)) { endpoint = ""; for (int i = 0; i < seg.Length - 1; i++) { endpoint += seg[i]; } } bool sup = validator.shallYouPass(userid, token, endpoint, verb, out log); if (!debugmode && !sup) { environment.Response.StatusCode = 401; environment.Response.Body = new MemoryStream(); var newBody = new MemoryStream(); newBody.Seek(0, SeekOrigin.Begin); var newContent = new StreamReader(newBody).ReadToEnd(); newContent += "You shall no pass."; environment.Response.Body = newBody; environment.Response.Write(newContent); //log = _context.AccessLogses.FirstOrDefault(x => x.Id == logId); } else { await next(); //log = _context.AccessLogses.FirstOrDefault(x => x.Id == logId); } } ); //app.UseStaticFiles(); ConfigureAuth(app); }
public async Task Invoke(HttpContext context, IAccessLoggerRepository accessLogerRepository) { var easternTimeZone = DateTimeZoneProviders.Tzdb["Europe/Istanbul"]; accessLogs = new AccessLogs() { Host = context.Request.Host.ToString(), Path = context.Request.Path, Scheme = context.Request.Scheme, QueryString = string.IsNullOrEmpty(context.Request.QueryString.Value) ? null : context.Request.QueryString.Value, WhoRequested = string.IsNullOrEmpty(context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value) ? null : context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, Time = Instant.FromDateTimeUtc(DateTime.UtcNow).InZone(easternTimeZone).ToDateTimeUnspecified() }; await LogRequest(context); await LogResponse(context); await accessLogerRepository.LogThisAccess(accessLogs); }
public bool shallYouPass(int id, string token, string path, string method, out AccessLogs log) { bool pass = true; path = path.EndsWith("/")? path.Substring(0, path.Length - 1):path; path = path.StartsWith("/")? path.Substring(1, path.Length - 1):path; bool ispublic = isPublic(path, method); bool isauthenticated = isAuthenticated(id, token); bool nedauth = nedAuth(path, method); bool hasaccess = hasAccess(id, path, method); if (nedauth && !isauthenticated) { pass = false; } if (!ispublic && !hasaccess && !isauthenticated) { pass = false; } Access access = _context.Accesses.FirstOrDefault(a => string.Equals(a.Path.ToUpper(), path.ToUpper()) && a.Method == method); if (access == null || access.Id != 19) { log = new AccessLogs(); log.Id = AccessLogs.GetNextId(_context); log.Method = method; log.Path = path; log.UserId = (id == 0 ? null : (int?)id); log.Success = pass; log.AccessId = access == null ? null : (int?)access.Id; _context.AccessLogses.Add(log); _context.SaveChanges(); } else { log = null; } return(pass); }
public ActionResult Index() { System.Diagnostics.Debug.WriteLine(new SQLiteDBContext()); TokenData tokenData = new TokenData { Id = 0, client = new MobileClient { Name = "Android", Type = ClientType.Mobile }, Key = "63nummnbynin" }; ViewBag.Title = "Registarion"; using (var DB = new AccessLogs()) { //DB.TokenDatas.Add(tokenData); // DB.SaveChanges(); } return(View("Index", tokenData)); }
public async Task LogThisAccess(AccessLogs model) { await _context.AccessLogs.AddAsync(model); await _context.SaveChangesAsync(); }