-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert inline code to snippet references (#32003)
- Loading branch information
Showing
8 changed files
with
234 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...re/security/authentication/identity-api-authorization/8samples/APIforSPA/APIforSPA.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" /> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
10 changes: 10 additions & 0 deletions
10
...rity/authentication/identity-api-authorization/8samples/APIforSPA/ApplicationDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class ApplicationDbContext : IdentityDbContext<IdentityUser> | ||
{ | ||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : | ||
base(options) | ||
{ } | ||
} |
25 changes: 25 additions & 0 deletions
25
...core/security/authentication/identity-api-authorization/8samples/APIforSPA/EmailSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace APIforSPA; | ||
|
||
sealed class EmailSender : IEmailSender | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public EmailSender(ILogger<EmailSender> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
public List<Email> Emails { get; set; } = new(); | ||
|
||
public Task SendEmailAsync(string email, string subject, string htmlMessage) | ||
{ | ||
_logger.LogWarning($"{email} {subject} {htmlMessage}"); | ||
Emails.Add(new(email, subject, htmlMessage)); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
sealed record Email(string Address, string Subject, string HtmlMessage); |
129 changes: 129 additions & 0 deletions
129
aspnetcore/security/authentication/identity-api-authorization/8samples/APIforSPA/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#define Version1 // Version1 / Version2 / Version3 / Version4 | ||
// Version2 = require email confirmation | ||
// Version3 = require admin role | ||
// Version4 = require authorization for Swagger | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace APIforSPA; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
// <snippetActivateAPIs> | ||
builder.Services.AddIdentityApiEndpoints<IdentityUser>() | ||
.AddEntityFrameworkStores<ApplicationDbContext>(); | ||
// </snippetActivateAPIs> | ||
|
||
// <snippetAppDbContext> | ||
builder.Services.AddDbContext<ApplicationDbContext>( | ||
options => options.UseInMemoryDatabase("AppDb")); | ||
// </snippetAppDbContext> | ||
|
||
// <snippetAddAuthorization> | ||
builder.Services.AddAuthorization(); | ||
// </snippetAddAuthorization> | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
#if Version2 | ||
// <snippetConfigureEmail> | ||
builder.Services.Configure<IdentityOptions>(options => | ||
{ | ||
options.SignIn.RequireConfirmedEmail = true; | ||
}); | ||
|
||
builder.Services.AddTransient<IEmailSender, EmailSender>(); | ||
// </snippetConfigureEmail> | ||
#endif | ||
|
||
var app = builder.Build(); | ||
|
||
// <snippetMapEndpoints> | ||
app.MapIdentityApi<IdentityUser>(); | ||
// </snippetMapEndpoints> | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
var summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
#if Version1 || Version2 || Version4 | ||
// <snippetRequireAuthorization> | ||
app.MapGet("/weatherforecast", (HttpContext httpContext) => | ||
{ | ||
var forecast = Enumerable.Range(1, 5).Select(index => | ||
new WeatherForecast | ||
{ | ||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = summaries[Random.Shared.Next(summaries.Length)] | ||
}) | ||
.ToArray(); | ||
return forecast; | ||
}) | ||
.WithName("GetWeatherForecast") | ||
.WithOpenApi() | ||
.RequireAuthorization(); | ||
// </snippetRequireAuthorization> | ||
#endif | ||
#if Version3 | ||
app.MapGet("/weatherforecast", (HttpContext httpContext) => | ||
{ | ||
var forecast = Enumerable.Range(1, 5).Select(index => | ||
new WeatherForecast | ||
{ | ||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = summaries[Random.Shared.Next(summaries.Length)] | ||
}) | ||
.ToArray(); | ||
return forecast; | ||
}) | ||
.WithName("GetWeatherForecast") | ||
.WithOpenApi() | ||
// <snippetRequireAdmin> | ||
.RequireAuthorization("Admin"); | ||
// </snippetRequireAdmin> | ||
#endif | ||
|
||
#if Version4 | ||
// <snippetSwaggerAuth> | ||
app.MapSwagger().RequireAuthorization(); | ||
// </snippetSwaggerAuth> | ||
#endif | ||
|
||
// <snippetLogout> | ||
app.MapPost("/logout", async (SignInManager<IdentityUser> signInManager, | ||
[FromBody] object empty) => | ||
{ | ||
if (empty != null) | ||
{ | ||
await signInManager.SignOutAsync(); | ||
return Results.Ok(); | ||
} | ||
return Results.Unauthorized(); | ||
}) | ||
.WithOpenApi() | ||
.RequireAuthorization(); | ||
// </snippetLogout> | ||
|
||
app.Run(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../security/authentication/identity-api-authorization/8samples/APIforSPA/WeatherForecast.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace APIforSPA; | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateOnly Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
...authentication/identity-api-authorization/8samples/APIforSPA/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
Oops, something went wrong.