4. What is ASP.NET Core?
leaner and modular architecture
tighter security
reduced servicing
improved performance
pay-for-what-you-use model.
5. Why is ASP.NET Core?
Integration of modern client-side frameworks and development workflows
A cloud-ready environment-based configuration system
Built-in dependency injection
New light-weight and modular HTTP request pipeline (No System.Web.dll
Ability to host on IIS or self-host in your own process
Built on .NET Core, which supports true side-by-side app versioning
Ships entirely as NuGet packages
6. public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel() //
.UseServer("Microsoft.AspNet.Server.Kestrel")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
8. Configure - how the ASP.NET application will respond to individual HTTP
requests.
ConfigureServices - method for configuring services that are used by your
application.
16. Simple logging
[Route("api/[controller]")]
public class TodoController : Controller
{
private readonly ITodoRepository _todoRepository;
private readonly ILogger<TodoController> _logger;
public TodoController(ITodoRepository todoRepository,
ILogger<TodoController> logger)
{
_todoRepository = todoRepository;
_logger = logger;
}
[HttpGet]
public IEnumerable<TodoItem> GetAll()
{
_logger.LogInformation(LoggingEvents.LIST_ITEMS, "Listing all items");
return _todoRepository.GetAll();
}
}
17. Thirdparty Providers
elmah.io - provider for the elmah.io service
Loggr - provider for the Loggr service
NLog - provider for the NLog library
Serilog - provider for the Serilog library
your own
27. Identity
// Authentication should be set to Individual User Accounts
// ConfigureServices
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Configure
app.UserIdentity();
// Manage users
UserManager<ApplicationUser> _usermanager;
// Manage sign in/sign out
SignInManage<ApplicationUser> _signinmanager;
28. Facebook, Twitter, Google Providers
app.UseFacebookAuthentication(new FacebookOptions()
{
AppId = Configuration["Authentication:Facebook:AppId"],
AppSecret = Configuration["Authentication:Facebook:AppSecret"]
});
// Other providers use similar approach
29. Other Features
Two-factor authentication with SMS
Supporting Third Party Clients using OAuth 2.0
Azure Active Directory
Securing ASP.NET Core apps with IdentityServer4
31. var builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddJsonFile("appsettings.json");
builder.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddCommandLine(args);
var connectionStringConfig = builder.Build();
var str = connectionStringConfig.GetConnectionString("DefaultConnection")
32. IOptions
// ConfigureServices
services.AddOptions();
services.Configure<MyOptions>(Configuration);
// Configure MyOptions using code
services.Configure<MyOptions>(myOptions =>
{
myOptions.Option1 = "value1_from_action";
myOptions.Option2 = connectionStringConfig.GetConnectionString("DefaultConnection");
});
public class HomeController : Controller
{
private readonly IOptions<MyOptions> _optionsAccessor;
public HomeController(IOptions<MyOptions> optionsAccessor)
{
_optionsAccessor = optionsAccessor;
}
// GET: /<controller>/
public IActionResult Index() => View(_optionsAccessor.Value);
}
public class MyOptions
{
public string Option1 { get; set;
}
public string Option2 { get; set;
}
}