This part 5 of 10 part series which outlines my
implementation of Multi-Tenant Claim Based Identity. For more details please
see my index post.
As you know Startup is used for configure the request pipeline
that handles all requests made to the application. I am using this Startup to
configure the following:
- Configure Identity Framework
- Dependency Injection of Identity classes and my
classes
- Automapper configuration (optional)
Here are the different sections of Startup. I have included
only relevant code.
Startup Constructor
This is standard code. In this I am saving the connection
string in a global variable so that I can later use it.
public Startup(IHostingEnvironment env)
{
//
Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
this.Configuration =
builder.Build();
//
saving the site connection string
SiteSettings.ConnectionString = this.Configuration.GetConnectionString("MegaMine");
}
ConfigureServices
In this method I am configuring the following services
- Configuring Entity Framework
- Configure ASP.NET Identity system
- Configure MVC
- Configure our Authorization filter
- Dependency injection configuration for classes
related to security
- Automapper configuration for classes related to
security
Here is the relevant code in this method
public void ConfigureServices(IServiceCollection services)
{
//
configuring Entity Framework
services.AddEntityFrameworkSqlServer()
.AddDbContext<SecurityDbContext>(options =>
{
options.UseSqlServer(SiteSettings.ConnectionString);
});
//
Configuring ASP.NET Identity
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<SecurityDbContext>()
.AddDefaultTokenProviders();
services.AddMvc()
.AddMvcOptions(options =>
{
options.Filters.Add(new NTAuthorizeFilter()); // Adding our Authorization filter
})
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver
= new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
});
//
dependency injection
services.AddScoped<SignInManager<ApplicationUser>, ApplicationSignInManager>();
services.AddTransient<AccountDomain>();
services.AddTransient<SecurityRepository>();
//
Automapper configurations
Mapper.Initialize(x =>
{
x.AddProfile<SecurityMappingProfile>();
});
}
Configure
In this Configure method, apart from standard request
processing configuration, I added the following configurations:
- Enable ASP.NET Identity
- Enable my own middleware which stores User
information in a custom request context
Here is the code for this configure method
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//
Middleware for storing context for the request
app.UseContextMiddleware();
//
To enable deliverying static content
app.UseStaticFiles();
//
Enabling ASP.NET Identity
app.UseIdentity();
//
Custom middleware to store user details
app.UseProfileMiddleware();
//
Enabling MVC
app.UseMvc(routes =>
{
routes.MapRoute(
name: "webapi",
template: "api/{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "error",
template: "Error",
defaults: new { controller = "Home", action = "Error" });
routes.MapRoute(
name: "default",
template: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" });
});
}
With the above Configurations in the Startup class, the application
is configure to utilize Multi-Tenant Claim Based Identity.