This file contains hidden or 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
services.AddSwaggerGen(x => | |
{ | |
x.SwaggerDoc("v1", new OpenApiInfo{ Title = "Tweetbook API", Version = "v1" }); | |
x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | |
{ | |
Description = "JWT Authorization header using the bearer scheme", | |
Name = "Authorization", | |
In = ParameterLocation.Header, | |
Type = SecuritySchemeType.ApiKey |
This file contains hidden or 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
public void Dispose() | |
{ | |
using var serviceScope = _serviceProvider.CreateScope(); | |
var context = serviceScope.ServiceProvider.GetService<DataContext>(); | |
context.Database.EnsureDeleted(); | |
} |
This file contains hidden or 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
services.AddAuthorization( options => | |
{ | |
options.AddPolicy("TagViewer", builder => builder.RequireClaim("tags.view", "true")); | |
}); |
This file contains hidden or 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
services.AddAuthorization( options => | |
{ | |
options.AddPolicy("TagViewer", builder => builder.RequireClaim("tags.view", "true")); | |
}); |
This file contains hidden or 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
using (var serviceScope = host.Services.CreateScope()) | |
{ | |
var roleManager = serviceScope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>(); | |
if (!await roleManager.RoleExistsAsync("Admin")) | |
{ | |
var adminRole = new IdentityRole("Admin"); | |
await roleManager.CreateAsync(adminRole); | |
} |
This file contains hidden or 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
public class WorksForCompanyHandler : AuthorizationHandler<WorksForCompanyRequirement> | |
{ | |
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, WorksForCompanyRequirement requirement) | |
{ | |
var userEmailAddress = context.User?.FindFirstValue(ClaimTypes.Email) ?? string.Empty; | |
if (userEmailAddress.EndsWith(requirement.DomainName)) | |
{ | |
context.Succeed(requirement); | |
return Task.CompletedTask; | |
} |
This file contains hidden or 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
public class WorksForCompanyRequirement : IAuthorizationRequirement | |
{ | |
public string DomainName { get; } | |
public WorksForCompanyRequirement(string domainName) | |
{ | |
DomainName = domainName; | |
} | |
} |
This file contains hidden or 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
services.AddAuthorization(options => | |
{ | |
options.AddPolicy("MustWorkForChapsas", policy => | |
{ | |
policy.AddRequirements(new WorksForCompanyRequirement("chapsas.com")); | |
}); | |
}); | |
services.AddSingleton<IAuthorizationHandler, WorksForCompanyHandler>(); |
This file contains hidden or 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
public class Post | |
{ | |
[Key] | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
public string UserId { get; set; } | |
[ForeignKey(nameof(UserId))] |