ASP.NET Core

ASP.NET Core is the open-source version of ASP.NET, that runs on Windows, Linux, macOS, and Docker.

→ docs, code

Learn

Fundamentals

Deep dive

Recipes

Swagger

Logging

Dispose

Performance

OData

Securing

Testing

Versioning

Health checks

Packaging

WebPack

Articles to review:

Controller action status code

public IActionResult Post([FromBody]string action)
{
    if (...)
    {
        return StatusCode(423);
    }

    return Ok(new ... {});
  }
}

Authentication

Use Azure Directory

<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.1.1" />
  • Edit appsettings.json file

"AzureAd": {
  "Instance": "https://login.microsoftonline.com/",
  "Domain": "<domainName>",
  "TenantId": "<tenantId>",
  "ClientId": "<clientId>",
  "CallbackPath": "/signin-oidc"
},
  • Edit Startup.cs:

// in ConfigureServices()

services.Configure<CookiePolicyOptions>(options =>
{
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

services
    .AddMvc(options =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        options.Filters.Add(new AuthorizeFilter(policy));
    })
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

// in Configure()

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseCookiePolicy();

app.UseAuthentication();

Last updated