-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Azure AD OIDC role-based authorization #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace EventTriangleAPI.Consumer.Presentation.Controllers; | ||
|
|
@@ -17,9 +18,23 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger) | |
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| [Authorize(Roles = "User, Admin")] | ||
| [HttpGet("user_and_admin")] | ||
| public IEnumerable<WeatherForecast> GetForUserAndAdmin() | ||
| { | ||
| return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
| { | ||
| Date = DateTime.Now.AddDays(index), | ||
| TemperatureC = Random.Shared.Next(-20, 55), | ||
| Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
| }) | ||
| .ToArray(); | ||
| } | ||
|
|
||
| [HttpGet(Name = "GetWeatherForecast")] | ||
| public IEnumerable<WeatherForecast> Get() | ||
| [Authorize(Roles = "Admin")] | ||
| [HttpGet("admin")] | ||
| public IEnumerable<WeatherForecast> GetForAdmin() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add to project package Swashbuckle.AspnetCore.Annotations and add [SwaggerOperation] attribute to controler methods |
||
| { | ||
| return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,31 @@ | ||
| var builder = WebApplication.CreateBuilder(args); | ||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
| using Microsoft.Identity.Web; | ||
| using Microsoft.IdentityModel.Logging; | ||
|
|
||
| // Add services to the container. | ||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddControllers(); | ||
| // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
| builder.Services.AddEndpointsApiExplorer(); | ||
| builder.Services.AddSwaggerGen(); | ||
|
|
||
| var configurationSection = builder.Configuration.GetSection("AzureAd"); | ||
|
|
||
| builder.Services | ||
| .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||
| .AddMicrosoftIdentityWebApi(configurationSection); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // Configure the HTTP request pipeline. | ||
| if (app.Environment.IsDevelopment()) | ||
| { | ||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(); | ||
| } | ||
| IdentityModelEventSource.ShowPII = true; | ||
|
|
||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Swagger UI config. For example: app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Event Triangle Consumer API V1");
});Don't forget about SwaggerGen configureation. For example: builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new OpenApiInfo { Title = "Event Triangle Consumer API", Version = "v1" });
}); |
||
|
|
||
|
|
||
| app.UseHttpsRedirection(); | ||
|
|
||
| app.UseAuthentication(); | ||
|
|
||
| app.UseAuthorization(); | ||
|
|
||
| app.MapControllers(); | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Identity.Web.Resource; | ||
|
|
||
| namespace EventTriangleAPI.Sender.Presentation.Controllers; | ||
|
|
||
| [ApiController] | ||
| [Route("[controller]")] | ||
| [RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")] | ||
| public class WeatherForecastController : ControllerBase | ||
| { | ||
| private static readonly string[] Summaries = new[] | ||
|
|
@@ -18,8 +21,22 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger) | |
| _logger = logger; | ||
| } | ||
|
|
||
| [HttpGet(Name = "GetWeatherForecast")] | ||
| public IEnumerable<WeatherForecast> Get() | ||
| [Authorize(Roles = "User, Admin")] | ||
| [HttpGet("user_and_admin")] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use "-" symbol instead of underscore |
||
| public IEnumerable<WeatherForecast> GetForUserAndAdmin() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add to project package Swashbuckle.AspnetCore.Annotations and add [SwaggerOperation] attribute to controler methods |
||
| { | ||
| return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
| { | ||
| Date = DateTime.Now.AddDays(index), | ||
| TemperatureC = Random.Shared.Next(-20, 55), | ||
| Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
| }) | ||
| .ToArray(); | ||
| } | ||
|
|
||
| [Authorize(Roles = "Admin")] | ||
| [HttpGet("admin")] | ||
| public IEnumerable<WeatherForecast> GetForAdmin() | ||
| { | ||
| return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| var builder = WebApplication.CreateBuilder(args); | ||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
| using Microsoft.Identity.Web; | ||
| using Microsoft.IdentityModel.Logging; | ||
|
|
||
| // Add services to the container. | ||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddControllers(); | ||
| // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
| builder.Services.AddEndpointsApiExplorer(); | ||
| builder.Services.AddSwaggerGen(); | ||
|
|
||
| var configurationSection = builder.Configuration.GetSection("AzureAd"); | ||
|
|
||
| builder.Services | ||
| .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||
| .AddMicrosoftIdentityWebApi(configurationSection); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // Configure the HTTP request pipeline. | ||
| if (app.Environment.IsDevelopment()) | ||
| { | ||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(); | ||
| } | ||
| IdentityModelEventSource.ShowPII = true; | ||
|
|
||
|
|
||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Swagger UI config. For example: app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Event Triangle Sender API V1");
});Don't forget about SwaggerGen configureation. For example: builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new OpenApiInfo { Title = "Event Triangle Sender API", Version = "v1" });
}); |
||
|
|
||
|
|
||
| app.UseHttpsRedirection(); | ||
|
|
||
| app.UseAuthentication(); | ||
|
|
||
| app.UseAuthorization(); | ||
|
|
||
| app.MapControllers(); | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use "-" symbol instead of underscore