public static void CustomCookieAuthentication(this IServiceCollection services) { services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.AccessDeniedPath = new PathString("/Account/Access"); options.Cookie = new CookieBuilder { //Domain = "", HttpOnly = true, Name = ".TCU.English.Security.Cookie", Path = "/", SameSite = SameSiteMode.Lax, SecurePolicy = CookieSecurePolicy.SameAsRequest, }; options.ExpireTimeSpan = TimeSpan.FromMinutes(Config.MAX_COOKIE_LIFE_MINUTES); // Thời hạn đăng nhập là 10 năm options.Events = new CookieAuthenticationEvents { OnSignedIn = context => { //Console.WriteLine("{0} - {1}: {2}", DateTime.Now, // "OnSignedIn", context.Principal.Identity.Name); return(Task.CompletedTask); }, OnSigningOut = context => { //Console.WriteLine("{0} - {1}: {2}", DateTime.Now, // "OnSigningOut", context.HttpContext.User.Identity.Name); return(Task.CompletedTask); }, OnValidatePrincipal = context => { //Console.WriteLine("{0} - {1}: {2}", DateTime.Now, // "OnValidatePrincipal", context.Principal.Identity.Name); return(Task.CompletedTask); } }; // Thời gian giới hạn cho một phiên đăng nhập //options.ExpireTimeSpan = TimeSpan.FromMinutes(10); options.LoginPath = new PathString($"/{NameUtils.ControllerName<AuthenticationController>()}"); options.ReturnUrlParameter = ReturnUrlParameter; options.SlidingExpiration = true; }); }
public static async Task <string> UploadImage(this IHostEnvironment _host, IFormFile file, params string[] subFolders) { if (file != null && file.Length > 0 && file.Length <= Config.MAX_IMAGE_SIZE) { if (MimeTypeUtils.Image.CheckContentType(file.ContentType) && MimeTypeUtils.Image.CheckFileExtension(file.FileName)) { // Upload avatar try { var uniqueFileName = NameUtils.GetUniqueFileName(file.FileName); var uploads = Path.Combine(_host.GetContentPathRootForUploadUtils(), NameUtils.ControllerName <UploadsController>().ToLower()); foreach (string subFolder in subFolders) { uploads = Path.Combine(uploads, subFolder); } uploads = Path.Combine(uploads, "images"); // Kiểm tra xem folder có tồn tại không? Nếu không thì tạo mới if (!Directory.Exists(uploads)) { Directory.CreateDirectory(uploads); } var filePath = Path.Combine(uploads, uniqueFileName); using (var stream = File.Create(filePath)) { await file.CopyToAsync(stream); } return(filePath.Replace(_host.GetContentPathRootForUploadUtils(), "")); } catch (Exception) { } } } return(""); }