· 5 years ago · Feb 23, 2020, 07:30 AM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using Microsoft.AspNetCore.Builder;
6using Microsoft.AspNetCore.Identity;
7using Microsoft.AspNetCore.Identity.UI;
8using Microsoft.AspNetCore.Hosting;
9using Microsoft.AspNetCore.Http;
10using Microsoft.AspNetCore.HttpsPolicy;
11using Microsoft.AspNetCore.Mvc;
12using Microsoft.EntityFrameworkCore;
13using PucAdmission.Data;
14using Microsoft.Extensions.Configuration;
15using Microsoft.Extensions.DependencyInjection;
16using Microsoft.AspNetCore.Identity.UI.Services;
17using PucAdmission.Services;
18using PaulMiami.AspNetCore.Mvc.Recaptcha;
19using AutoMapper;
20using PucAdmission.Data.Manager;
21using Microsoft.AspNetCore.Rewrite;
22using PucAdmission.Data.Student;
23using System.Globalization;
24using Microsoft.AspNetCore.Authentication.Cookies;
25
26namespace PucAdmission
27{
28 public class Startup
29 {
30 public Startup(IConfiguration configuration)
31 {
32 Configuration = configuration;
33 }
34
35 public IConfiguration Configuration { get; }
36
37 // This method gets called by the runtime. Use this method to add services to the container.
38 public void ConfigureServices(IServiceCollection services)
39 {
40 services.Configure<CookiePolicyOptions>(options =>
41 {
42 // This lambda determines whether user consent for non-essential cookies is needed for a given request.
43 options.CheckConsentNeeded = context => true;
44 options.MinimumSameSitePolicy = SameSiteMode.None;
45 });
46
47 services.Configure<IdentityOptions>(options =>
48 {
49 // Default Password settings.
50 options.Password.RequireDigit = true;
51 options.Password.RequireLowercase = true;
52 options.Password.RequireNonAlphanumeric = true;
53 options.Password.RequireUppercase = true;
54 options.Password.RequiredLength = 6;
55 options.Password.RequiredUniqueChars = 1;
56
57 options.User.RequireUniqueEmail = true;
58 });
59
60
61 services.AddDbContext<ApplicationDbContext>(options =>
62 options.UseSqlServer(
63 Configuration.GetConnectionString("DefaultConnection")));
64
65 services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>()
66 .AddDefaultUI(UIFramework.Bootstrap4)
67 .AddDefaultTokenProviders()
68 .AddEntityFrameworkStores<ApplicationDbContext>();
69
70 services.Configure<RequestLocalizationOptions>(options =>
71 {
72 options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-IN");
73 options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-IN") };
74 });
75
76 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
77
78 services.AddTransient<IEmailSender, EmailSender>();
79 services.AddTransient<ISmsSender, SmsSender>();
80 services.AddTransient<IDocumentService, DocumentService>();
81 services.AddTransient<IPayUMoney, PayUMoney>();
82 services.AddTransient<IManagerRepository, ManagerRepository>();
83 services.AddTransient<IStudentRepository, StudentRepository>();
84 services.Configure<SmtpConfig>(Configuration.GetSection("SMTPsettings"));
85 services.Configure<SmsConfig>(Configuration.GetSection("SMSsettings"));
86 services.Configure<PayUConfig>(Configuration.GetSection("PayUsettings"));
87
88
89 services.Configure<MvcOptions>(options =>
90 {
91 options.Filters.Add(new RequireHttpsAttribute());
92 });
93
94 //services.AddHttpContextAccessor();
95
96 //ReCaptchaV2 Service
97 services.AddRecaptcha(new RecaptchaOptions
98 {
99 SiteKey = Configuration["Recaptcha:SiteKey"],
100 SecretKey = Configuration["Recaptcha:SecretKey"]
101 });
102
103 services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
104
105
106 var config = new AutoMapper.MapperConfiguration(
107 cfg =>
108 {
109 cfg.AddProfile(new MappingProfile());
110 });
111
112 var mapper = config.CreateMapper();
113 services.AddSingleton(mapper);
114
115 services.ConfigureApplicationCookie(options =>
116 {
117 options.AccessDeniedPath = "/Identity/Account/AccessDenied";
118 options.Cookie.Name = "PucAdmission";
119 options.Cookie.HttpOnly = true;
120 options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
121 options.LoginPath = "/Identity/Account/Login";
122 // ReturnUrlParameter requires
123 //using Microsoft.AspNetCore.Authentication.Cookies;
124 options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
125 options.SlidingExpiration = true;
126 });
127 }
128
129 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
130 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
131 {
132 app.UseRequestLocalization();
133
134 if (env.IsDevelopment())
135 {
136 app.UseDeveloperExceptionPage();
137 app.UseDatabaseErrorPage();
138 }
139 else
140 {
141 app.UseExceptionHandler("/Home/Error");
142 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
143 app.UseHsts();
144 }
145
146 app.UseHttpsRedirection();
147 app.UseStaticFiles();
148
149 app.UseAuthentication();
150
151 app.UseMvc(routes =>
152 {
153 routes.MapRoute(
154 name: "default",
155 template: "{controller=Home}/{action=Index}/{id?}");
156 });
157 app.UseCookiePolicy();
158 var options = new RewriteOptions().AddRedirectToHttps();
159
160 app.UseRewriter(options);
161 }
162 }
163}