public static void SetIdentityOptions(string solutionDirectory, AuthSetup setup) { var classPath = ClassPathHelper.IdentityProjectPath(solutionDirectory, $"ServiceExtensions.cs"); if (!Directory.Exists(classPath.ClassDirectory)) { throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found."); } if (!File.Exists(classPath.FullClassPath)) { throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found."); } var tempPath = $"{classPath.FullClassPath}temp"; using (var input = File.OpenText(classPath.FullClassPath)) { using (var output = new StreamWriter(tempPath)) { string line; while (null != (line = input.ReadLine())) { var newText = $"{line}"; if (line.Contains($"options.User.RequireUniqueEmail")) { newText = @$ " options.User.RequireUniqueEmail = {setup.IdentityRequirements.RequireUniqueEmail.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequiredLength")) { newText = @$ " options.Password.RequiredLength = {setup.IdentityRequirements.RequiredLength};"; } else if (line.Contains($"options.Password.RequireDigit")) { newText = @$ " options.Password.RequireDigit = {setup.IdentityRequirements.RequireDigit.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequireLowercase")) { newText = @$ " options.Password.RequireLowercase = {setup.IdentityRequirements.RequireLowercase.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequireUppercase")) { newText = @$ " options.Password.RequireUppercase = {setup.IdentityRequirements.RequireUppercase.ToString().ToLower()};"; } else if (line.Contains($"options.Password.RequireNonAlphanumeric")) { newText = @$ " options.Password.RequireNonAlphanumeric = {setup.IdentityRequirements.RequireNonAlphanumeric.ToString().ToLower()};"; } output.WriteLine(newText); } } } // delete the old file and set the name of the new one to the original name File.Delete(classPath.FullClassPath); File.Move(tempPath, classPath.FullClassPath); GlobalSingleton.AddUpdatedFile(classPath.FullClassPath.Replace($"{solutionDirectory}{Path.DirectorySeparatorChar}", "")); }
public ActionResult ProcessAuth(AuthSetup auth) { try { var db = new InlaksBIContext(); var oldauth = db.AuthConfig.First(); oldauth.AuthType = auth.AuthType; oldauth.Server = auth.Server; db.SaveChanges(); ViewBag.message = auth.AuthType + " Authentication Mode Activated Successfully"; ViewBag.errorclass = "green"; } catch (Exception e) { ViewBag.message = "Failed to apply changes, please seek technical assistance"; ViewBag.errorclass = "red"; } return(View("AuthSetup", auth)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Set up database context services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.Configure <EmailSettings>(config => Configuration.GetSection("EmailSettings").Bind(config)); services.Configure <ErrorEmailSettings>(config => Configuration.GetSection("ErrorEmailSettings").Bind(config)); // Set up identity services.AddIdentity <User, Role>(config => { config.SignIn.RequireConfirmedEmail = true; config.Password.RequireNonAlphanumeric = false; config.Password.RequireUppercase = false; config.Password.RequireLowercase = false; config.User.RequireUniqueEmail = false; config.Lockout.MaxFailedAccessAttempts = int.TryParse(Configuration["MaxFailedAccessAttempts"], out var maxFailed) ? maxFailed : 5; }) .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); // Set up authentication services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(cfg => { cfg.RequireHttpsMetadata = false; cfg.SaveToken = true; cfg.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = Configuration["JwtTokens:Issuer"], ValidAudience = Configuration["JwtTokens:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtTokens:Key"])) }; }); // .AddCookie(cfg => cfg.SlidingExpiration = true); // Set up authorization and permission AuthSetup.ConfigureAuthorization(services); // Set up session services.AddDistributedMemoryCache(); services.AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(30)); // Add application services. ConfigureBusinessServices(services); // Register the Swagger generator, defining one or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "ComplyTo Compliance Cloud Product API v1", Version = "v1" }); }); // Add custom antiforgery token for Angular services.AddAntiforgery(opts => { opts.HeaderName = "X-XSRF-TOKEN"; opts.SuppressXFrameOptionsHeader = true; }); services.AddTransient <AntiforgeryCookieResultFilter>(); // Add Mvc services.AddMvc(); // Add httpcontext services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); if (CurrentEnvironment.IsProduction()) { services.Configure <MvcOptions>(options => { options.Filters.Add(new RequireHttpsAttribute()); }); } }