徐明 преди 5 години
родител
ревизия
4b5b77a3b9

+ 39 - 1
CasinosManager.Api/CasinosManager.Api/Controllers/IdentityController.cs

@@ -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;
+        }
     }
 }

+ 37 - 0
CasinosManager.Api/CasinosManager.Api/Dto/AuthenticateModel.cs

@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace CasinosManager.Api.Dto
+{
+    public class AuthenticateModel
+    {
+        public string username { get; set; }
+        public string password { get; set; }
+        public string client_id { get; set; }
+        public string client_secret { get; set; }
+        public string grant_type { get; set; }
+        public string scope { get; set; }
+
+        public AuthenticateModel()
+        {
+            this.client_id = "angular.client";
+            this.client_secret = "secret";
+            this.grant_type = "password";
+            this.scope = "CasinosApi";
+        }
+    }
+
+    public class AuthenticateResult
+    {
+        public string access_token { get; set; }
+        public int expires_in { get; set; }
+        public string token_type { get; set; }
+
+        public AuthenticateResult()
+        {
+
+        }
+    }
+}