Skip to content
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

144 lines (124 sloc) 6.19 KB
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using CommonCache;
using DataAuthorize;
using DataLayer.EfCode;
using FeatureAuthorize;
using FeatureAuthorize.PolicyCode;
using GenericServices.Setup;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PermissionAccessControl2.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceLayer.AppStart;
using ServiceLayer.CodeCalledInStartup;
using ServiceLayer.UserServices;
using Swashbuckle.AspNetCore.Swagger;
namespace PermissionAccessControl2
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.Configure<CookiePolicyOptions>(options =>
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false; //!!!!!!!!!!!!!!!!!!!!!! Turned off
options.MinimumSameSitePolicy = SameSiteMode.None;
//This registers the various databases, either as in-memory or via SQL Server (see appsetting.json for connection strings)
//This enables Cookies for authentication and adds the feature and data claims to the user
services.ConfigureCookiesForExtraAuth(Configuration["DemoSetup:UpdateCookieOnChange"] == "True");
services.AddSingleton(Configuration); //Needed for SuperAdmin setup
//Register the Permission policy handlers
services.AddSingleton<IAuthorizationPolicyProvider, AuthorizationPolicyProvider>();
services.AddSingleton<IAuthorizationHandler, PermissionHandler>();
//This is needed to implement the data authorize code
services.AddScoped<IGetClaimsProvider, GetClaimsFromUser>();
//This registers the services into DI
//This has to come after the ConfigureCookiesForExtraAuth settings, which sets up the IAuthChanges
services.ConfigureGenericServicesEntities(typeof(ExtraAuthorizeDbContext), typeof(CompanyDbContext))
//I add Swagger so that you can test the FrontEndController that provides the permissions of the current user
services.AddSwaggerGen(c =>
c.SwaggerDoc("v1", new Info { Title = "My API V1", Version = "v1" });
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (!File.Exists(xmlPath))
throw new InvalidOperationException("The XML file does not exist for Swagger - see link above for more info.");
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
if (env.IsDevelopment())
// Enable middleware to serve generated Swagger as a JSON endpoint.
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see
//This should come AFTER the app.UseAuthentication() call
if (Configuration["DemoSetup:UpdateCookieOnChange"] == "True")
//If UpdateCookieOnChange this adds a header which has the time that the user's claims were updated
//thanks to
app.Use((context, next) =>
var lastTimeUserPermissionsSet = context.User.Claims
.SingleOrDefault(x => x.Type == PermissionConstants.LastPermissionsUpdatedClaimType)?.Value;
if (lastTimeUserPermissionsSet != null)
context.Response.Headers["Last-Time-Users-Permissions-Updated"] = lastTimeUserPermissionsSet;
return next.Invoke();
app.UseMvc(routes =>
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
You can’t perform that action at this time.