Skip to content

Instantly share code, notes, and snippets.

@abhishekluv
Created February 10, 2020 12:02
Show Gist options
  • Save abhishekluv/20bb0fa049fde2d053a59244f18fb7fa to your computer and use it in GitHub Desktop.
Save abhishekluv/20bb0fa049fde2d053a59244f18fb7fa to your computer and use it in GitHub Desktop.
ASP.NET Core Source Code Demo 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ASPNETCoreDay11.Models;
namespace ASPNETCoreDay11
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddIdentity<CustomUser, IdentityRole>(cfg =>
{
cfg.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<DatabaseContext>();
services.AddDbContextPool<DatabaseContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("EFCoreDbConnection"));
});
//configurations for cookies
services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always;
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
options.ExpireTimeSpan = TimeSpan.FromMinutes(10); // cookie expires in 10mins
options.SlidingExpiration = false; // grace time false means no grace time
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); // make sure that we call the UseAuthentication middleware in our http request pipeline
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment