Skip to content

Instantly share code, notes, and snippets.

@vmandic
Created May 17, 2019 12:38
Show Gist options
  • Save vmandic/31f9956bcb8407644a61e5c001801c30 to your computer and use it in GitHub Desktop.
Save vmandic/31f9956bcb8407644a61e5c001801c30 to your computer and use it in GitHub Desktop.
med-processor, part/4, snippet #17
using MedsProcessor.WebAPI.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace MedsProcessor.WebAPI.Controllers
{
[ApiVersionNeutral, Route("api/[controller]")]
public class AuthController : ApiControllerBase
{
private readonly IJwtAuthService jwtService;
public AuthController(IJwtAuthService jwtService)
{
this.jwtService = jwtService;
}
[AllowAnonymous, HttpPost("token")]
public ActionResult<AuthTokenResponse> RequestToken([FromBody] AuthTokenRequest request)
{
var(authenticatedSuccessfully, token) = jwtService.IssueToken(request);
return authenticatedSuccessfully
? ApiResponse.ForData(
new AuthTokenResponse(token),
message: "Access token issued successfully.")
: ApiResponse.ForData(
new AuthTokenResponse(),
StatusCodes.Status401Unauthorized,
message: "An invalid or unauthorized Client ID was provided.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment