|
@@ -1,16 +1,18 @@
|
|
|
using CasinosManager.Api.Dto;
|
|
|
using IdentityModel;
|
|
|
+using IdentityModel.Client;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
namespace CasinosManager.Api.Controllers
|
|
|
{
|
|
|
[Route("api/[controller]")]
|
|
|
[ApiController]
|
|
|
- [Authorize]
|
|
|
public class IdentityController : ControllerBase
|
|
|
{
|
|
|
[HttpGet()]
|
|
|
+ [Authorize]
|
|
|
public ActionResult<CurrentUserInfo> Get()
|
|
|
{
|
|
|
CurrentUserInfo userInfo = new CurrentUserInfo();
|
|
@@ -40,5 +42,41 @@ namespace CasinosManager.Api.Controllers
|
|
|
|
|
|
return userInfo;
|
|
|
}
|
|
|
+
|
|
|
+ [HttpPost()]
|
|
|
+ public async Task<ActionResult<AuthenticateResult>> Login(AuthenticateModel model)
|
|
|
+ {
|
|
|
+ AuthenticateResult result = null;
|
|
|
+
|
|
|
+ var disco = await DiscoveryClient.GetAsync("http://localhost:53002");
|
|
|
+ if (disco.IsError)
|
|
|
+ {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // request token
|
|
|
+ var tokenClient = new TokenClient(disco.TokenEndpoint, model.client_id, model.client_secret);
|
|
|
+ var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(model.username, model.password, model.scope);
|
|
|
+
|
|
|
+ if (tokenResponse.IsError)
|
|
|
+ {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ result = new AuthenticateResult();
|
|
|
+ result.access_token = tokenResponse.AccessToken;
|
|
|
+ result.expires_in = tokenResponse.ExpiresIn;
|
|
|
+ result.token_type = tokenResponse.TokenType;
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpDelete]
|
|
|
+ [Authorize]
|
|
|
+ public async Task<bool> LogoutAsync()
|
|
|
+ {
|
|
|
+ await HttpContext.Authentication.SignOutAsync("idsrv.session");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|