ScientificReport
Startup.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using Microsoft.AspNetCore.Authentication.Cookies;
5 using Microsoft.AspNetCore.Builder;
6 using Microsoft.AspNetCore.Identity;
7 using Microsoft.AspNetCore.Hosting;
8 using Microsoft.AspNetCore.Http;
9 using Microsoft.AspNetCore.Localization;
10 using Microsoft.AspNetCore.Mvc;
11 using Microsoft.AspNetCore.Mvc.Razor;
12 using Microsoft.EntityFrameworkCore;
13 using Microsoft.Extensions.Configuration;
14 using Microsoft.Extensions.DependencyInjection;
15 using Rotativa.AspNetCore;
16 using Microsoft.Extensions.Options;
22 
23 namespace ScientificReport
24 {
25  public class Startup
26  {
27  public Startup(IConfiguration configuration)
28  {
29  Configuration = configuration;
30  }
31 
32  private IConfiguration Configuration { get; }
33 
34  // This method gets called by the runtime. Use this method to add services to the container.
35  public void ConfigureServices(IServiceCollection services)
36  {
37  services.Configure<CookiePolicyOptions>(options =>
38  {
39  // This lambda determines whether user consent for non-essential cookies is needed for a given request.
40  options.CheckConsentNeeded = context => true;
41  options.MinimumSameSitePolicy = SameSiteMode.None;
42  });
43 
44  services.AddDbContext<ScientificReportDbContext>(options =>
45  options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"),
46  b => b.MigrationsAssembly("ScientificReport"))
47  );
48 
49  services.AddIdentity<UserProfile, UserProfileRole>()
50  .AddEntityFrameworkStores<ScientificReportDbContext>()
51  .AddDefaultTokenProviders();
52 
53  services.AddTransient<IUserProfileService, UserProfileService>();
54  services.AddTransient<IDepartmentService, DepartmentService>();
55  services.AddTransient<IScientificWorkService, ScientificWorkService>();
56  services.AddTransient<ITeacherReportService, TeacherReportService>();
57  services.AddTransient<IArticleService, ArticleService>();
58  services.AddTransient<IConferenceService, ConferenceService>();
59  services.AddTransient<IGrantService, GrantService>();
60  services.AddTransient<IMembershipService, MembershipService>();
61  services.AddTransient<IOppositionService, OppositionService>();
65  services.AddTransient<IPublicationService, PublicationService>();
66  services.AddTransient<IReportThesisService, ReportThesisService>();
67  services.AddTransient<IReviewService, ReviewService>();
70 
71  services.Configure<IdentityOptions>(options =>
72  {
73  // Password settings.
74  // TODO: configure password setting properly for deploy
75  options.Password.RequireDigit = false;
76  options.Password.RequireLowercase = false;
77  options.Password.RequireNonAlphanumeric = false;
78  options.Password.RequireUppercase = false;
79  options.Password.RequiredLength = 3;
80  options.Password.RequiredUniqueChars = 0;
81 
82  // Lockout settings.
83  options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromDays(1);
84  options.Lockout.MaxFailedAccessAttempts = 5;
85  options.Lockout.AllowedForNewUsers = true;
86 
87  // User settings.
88  options.User.AllowedUserNameCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._";
89  options.User.RequireUniqueEmail = true;
90  });
91 
92  services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
93  services.ConfigureApplicationCookie(options =>
94  {
95  // Cookie settings
96  options.Cookie.HttpOnly = true;
97  options.ExpireTimeSpan = TimeSpan.FromDays(1);
98  options.LoginPath = "/UserProfile/Login";
99  options.AccessDeniedPath = "/Home/AccessDenied";
100  options.SlidingExpiration = true;
101  });
102 
103  services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
104  services.Configure<RequestLocalizationOptions>(
105  opts =>
106  {
107  var supportedCultures = new List<CultureInfo>
108  {
109  new CultureInfo("uk-UA"),
110  new CultureInfo("en")
111  };
112 
113  opts.DefaultRequestCulture = new RequestCulture("en");
114  // Formatting numbers, dates, etc.
115  opts.SupportedCultures = supportedCultures;
116  // UI strings that we have localized.
117  opts.SupportedUICultures = supportedCultures;
118  });
119 
120  services.AddMvc()
121  .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
122  .AddViewLocalization(
123  LanguageViewLocationExpanderFormat.Suffix,
124  opts => { opts.ResourcesPath = "Resources"; })
125  .AddDataAnnotationsLocalization();
126  }
127 
128  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
129  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
130  {
131  if (env.IsDevelopment())
132  {
133  app.UseDeveloperExceptionPage();
134  app.UseDatabaseErrorPage();
135  }
136  else
137  {
138  app.UseExceptionHandler("/Home/Error");
139  app.UseHsts();
140  }
141  app.UseHttpsRedirection();
142  app.UseStaticFiles();
143  app.UseCookiePolicy();
144 
145  app.UseAuthentication();
146 
147  var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
148  app.UseRequestLocalization(options.Value);
149 
150  RotativaConfiguration.Setup(env);
151 
152  app.UseMvc(routes =>
153  {
154  routes.MapRoute(
155  "default",
156  "{controller=Home}/{action=Index}/{id?}");
157  });
158  }
159  }
160 }
Startup(IConfiguration configuration)
Definition: Startup.cs:27
void ConfigureServices(IServiceCollection services)
Definition: Startup.cs:35
void Configure(IApplicationBuilder app, IHostingEnvironment env)
Definition: Startup.cs:129