internal LoginToken GenerateOrGetToken(ApplicationUser user) { var tokens = _context.GetFullTable <LoginToken>(); if (tokens.Any((lt) => lt.User.UserName == user.UserName)) { var token = tokens.Where((lt) => lt.User.UserName == user.UserName).Single(); if (token.Valid > DateTime.Now) { return(token); } else { token.Token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); token.Valid = DateTime.Now.AddMinutes(_tokenLifespanInMinutes); _context.SaveChanges(); return(token); } } else { var token = new LoginToken() { Token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()), User = user, Valid = DateTime.Now.AddMinutes(_tokenLifespanInMinutes) }; _context.Tokens.Add(token); _context.SaveChanges(); return(token); } }
public IActionResult PutLecture([FromRoute] string id, [FromBody] Lecture lecture) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else if (!lectureExists(id)) { return(NotFound(id)); } var item = _context.Lectures.First(li => li.ID == id); try { item.Name = (lecture.Name != null && lecture.Name != item.Name) ? lecture.Name : item.Name; item.Version++; _context.Update(item); _context.SaveChanges(); } catch (DbUpdateConcurrencyException dbce) { Tracer.TraceMessage(dbce.Message); return(StatusCode(500)); } return(Ok(item)); }
public IActionResult UploadArtifact([FromRoute] string id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else if (!artifactExists(id)) { return(NotFound()); } long size = 0; try { var artifact = _context.Artifacts.Single((art) => art.ID == id); string dirPath = Path.Combine(_storagePath, artifact.ID); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var files = Request.Form.Files; var sha2 = System.Security.Cryptography.SHA256.Create(); foreach (var file in files) { var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Value.Trim('"'); filename = Path.Combine(dirPath, filename); size += file.Length; var hashValue = ""; if (!IOF.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.CreateNew)) { file.CopyTo(fs); fs.Flush(); } hashValue = ComputeHash(sha2, filename); _context.ArtifactStorage.Add(new ArtifactStorageItem() { ArtifactRef = artifact, Filename = Path.GetFileName(filename), Hash = hashValue }); _context.SaveChanges(); } else if (!artifact.StorageItems.Any(asi => asi.Filename == Path.GetFileName(filename))) { hashValue = ComputeHash(sha2, filename); _context.ArtifactStorage.Add(new ArtifactStorageItem() { ArtifactRef = artifact, Filename = Path.GetFileName(filename), Hash = hashValue }); _context.SaveChanges(); } } _context.Artifacts.Update(artifact); _context.SaveChanges(); } catch (Exception) { throw; } return(Ok(size)); }
/// <summary> /// Service-Configuration called by .NetCore Runtime /// </summary> /// <param name="services"></param> public void ConfigureServices(IServiceCollection services) { services.AddAntiforgery(options => { options.Cookie.Name = "_af"; options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always; options.HeaderName = "X-XSRF-TOKEN"; }); services.AddMvc(); services.Configure <FormOptions>(config => { config.ValueLengthLimit = int.MaxValue; config.MultipartBodyLengthLimit = uint.MaxValue; config.MultipartBoundaryLengthLimit = int.MaxValue; }); services.Configure <LdapConfig>(Configuration.GetSection("ldap_auth")); var context = new APIDatabaseContext(); /*if (Environment.IsDevelopment()) * context.Database.EnsureDeleted();*/ context.Database.EnsureCreated(); context.SaveChanges(); services.AddCors(options => options.AddPolicy("Automatic", builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin())); services.AddSingleton(Environment); #if !DEMO LdapConfig conf = new LdapConfig(); Configuration.Bind("ldap_auth", conf); services.AddScoped <IAuthenticationService>((service) => new LdapAuthenticationService(conf)); #else services.AddScoped <IAuthenticationService>((a) => new SimpleAuthenticationService()); #endif var signatureSection = Configuration.GetSection("SignatureService"); var certFile = ""; var certFilePassword = ""; var signatureEnabled = false; if (signatureSection != null) { signatureEnabled = signatureSection.GetValue("Enabled", false); certFile = signatureSection.GetValue("Certificate", string.Empty); certFilePassword = signatureSection.GetValue("Password", string.Empty); services.AddSingleton <ISignatureService>((service) => { var rsaSignatureService = new RSASignatureService(signatureEnabled); rsaSignatureService.LoadCertificate(certFile, certFilePassword); return(rsaSignatureService); }); } services.AddRouting(/*options => options.LowercaseUrls = true*/); services.AddDbContext <APIDatabaseContext>(ServiceLifetime.Scoped); /* * services.AddSwaggerGen(config => * { * config.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Project API", Version = "v1" }); * config.IncludeXmlComments(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "ProjectAPI.xml")); * });*/ }