From 7885b71dad3a2dea21b64532f6a825ce033bf0a6 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 16 Nov 2022 19:57:13 -0600 Subject: AƱadidos toques finales MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/MessageController.cs | 10 ++++++++-- Controllers/UsersController.cs | 8 ++++++++ Controllers/WeatherForecastController.cs | 2 +- Program.cs | 10 ++++++---- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Controllers/MessageController.cs b/Controllers/MessageController.cs index bacaf5c..2f4a8d8 100644 --- a/Controllers/MessageController.cs +++ b/Controllers/MessageController.cs @@ -9,9 +9,15 @@ namespace IdentityAPI.Controllers { public class MessageController : ControllerBase { public MessageController() {} - [HttpGet] - public async Task Show() { + [HttpGet("message_authenticated")] + public IActionResult AuthenticationMessage() { return Ok(new { message = "Authentication succesful" }); } + + [HttpGet("message_anonymous")] + [AllowAnonymous] + public IActionResult AnonymousMessage() { + return Ok(new { message = "Anonymous detected." }); + } } } \ No newline at end of file diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs index d5dded5..5483a6b 100644 --- a/Controllers/UsersController.cs +++ b/Controllers/UsersController.cs @@ -54,6 +54,14 @@ namespace IdentityAPI.Controllers { return StatusCode(400, new { error = "Invalid request body" }); } + [HttpGet("refresh")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + public async Task Refresh() { + var user = await _manager.FindByEmailAsync(HttpContext.User.Claims.Where(c => c.Type.Contains("email")).FirstOrDefault().Value); + + return Ok(new { Token = GenerateToken(user) }); + } + private string GenerateToken(IdentityUser user) { var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); diff --git a/Controllers/WeatherForecastController.cs b/Controllers/WeatherForecastController.cs index f7fdcdf..1a64e77 100644 --- a/Controllers/WeatherForecastController.cs +++ b/Controllers/WeatherForecastController.cs @@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization; namespace IdentityAPI.Controllers; [ApiController] -[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [Route("[controller]")] public class WeatherForecastController : ControllerBase { @@ -22,6 +21,7 @@ public class WeatherForecastController : ControllerBase } [HttpGet(Name = "GetWeatherForecast")] + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public IEnumerable Get() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast diff --git a/Program.cs b/Program.cs index a69992d..aa8c742 100644 --- a/Program.cs +++ b/Program.cs @@ -83,10 +83,12 @@ builder.Services.Configure(options => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); -// Identity configuration. -// builder.Services.Configure(options => { - -// }) +// CORS configuration +builder.Services.AddCors(options => { + options.AddDefaultPolicy(builder => { + builder.WithOrigins("https://apirequest.io").AllowAnyMethod().AllowAnyHeader(); + }); + }); var app = builder.Build(); -- cgit v1.2.3