From 7024bc9666e62cc33f651e7a060235d17bd51f25 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 14 Feb 2019 23:05:04 +0800 Subject: Develop user token interface. --- Timeline/Controllers/TestController.cs | 34 ---------------------------- Timeline/Controllers/UserController.cs | 36 +++++++++++++++++++++++++----- Timeline/Controllers/UserTestController.cs | 34 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 39 deletions(-) delete mode 100644 Timeline/Controllers/TestController.cs create mode 100644 Timeline/Controllers/UserTestController.cs (limited to 'Timeline/Controllers') diff --git a/Timeline/Controllers/TestController.cs b/Timeline/Controllers/TestController.cs deleted file mode 100644 index 1563830c..00000000 --- a/Timeline/Controllers/TestController.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace Timeline.Controllers -{ - [Route("api/[controller]")] - public class TestController : Controller - { - [HttpGet("[action]")] - [Authorize] - public string Action1() - { - return "test"; - } - - [HttpGet("[action]")] - [Authorize(Roles = "User,Admin")] - public string Action2() - { - return "test"; - } - - [HttpGet("[action]")] - [Authorize(Roles = "Admin")] - public string Action3() - { - return "test"; - } - } -} diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 9d6970e7..1ffed22b 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using System.IO; +using Timeline.Entities; using Timeline.Services; namespace Timeline.Controllers @@ -20,10 +23,15 @@ namespace Timeline.Controllers public string Password { get; set; } } - public class LoginInfo + public class CreateTokenResult + { + public string Token { get; set; } + public UserInfo UserInfo { get; set; } + } + + public class TokenValidationRequest { public string Token { get; set; } - public string[] Roles { get; set; } } private readonly IUserService _userService; @@ -39,7 +47,7 @@ namespace Timeline.Controllers [HttpPost("[action]")] [AllowAnonymous] - public ActionResult LogIn([FromBody] UserCredentials credentials) + public ActionResult CreateToken([FromBody] UserCredentials credentials) { var user = _userService.Authenticate(credentials.Username, credentials.Password); @@ -50,13 +58,31 @@ namespace Timeline.Controllers _logger.LogInformation(LoggingEventIds.LogInSucceeded, "Login with username: {} succeeded.", credentials.Username); - var result = new LoginInfo + var result = new CreateTokenResult { Token = _jwtService.GenerateJwtToken(user), - Roles = user.Roles + UserInfo = user.GetUserInfo() }; return Ok(result); } + + [HttpPost("[action]")] + [Consumes("text/plain")] + [AllowAnonymous] + public ActionResult ValidateToken([FromBody] string token) + { + var result = _jwtService.ValidateJwtToken(token); + return Ok(result); + } + + [HttpPost("[action]")] + [Consumes("application/json")] + [AllowAnonymous] + public ActionResult ValidateToken([FromBody] TokenValidationRequest request) + { + var result = _jwtService.ValidateJwtToken(request.Token); + return Ok(result); + } } } diff --git a/Timeline/Controllers/UserTestController.cs b/Timeline/Controllers/UserTestController.cs new file mode 100644 index 00000000..7fb6850b --- /dev/null +++ b/Timeline/Controllers/UserTestController.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Timeline.Controllers +{ + [Route("api/test/User")] + public class UserTestController : Controller + { + [HttpGet("[action]")] + [Authorize] + public ActionResult NeedAuthorize() + { + return Ok(); + } + + [HttpGet("[action]")] + [Authorize(Roles = "User,Admin")] + public ActionResult BothUserAndAdmin() + { + return Ok(); + } + + [HttpGet("[action]")] + [Authorize(Roles = "Admin")] + public ActionResult OnlyAdmin() + { + return Ok(); + } + } +} -- cgit v1.2.3