xf 2 年之前
父節點
當前提交
73b70098b5

+ 11 - 0
src/Hotline.Api/Context/ClaimExtensions.cs

@@ -0,0 +1,11 @@
+using System.Security.Claims;
+
+namespace Hotline.Api.Token;
+
+public static class ClaimExtensions
+{
+    public static string? FindFirstValue(this ClaimsPrincipal user, string claimType)
+    {
+        return user.Claims.FirstOrDefault(d => d.Type == claimType)?.Value;
+    }
+}

+ 4 - 14
src/Hotline.Api/Token/DefaultSessionContext.cs → src/Hotline.Api/Context/DefaultSessionContext.cs

@@ -13,12 +13,12 @@ namespace Hotline.Api.Token
             var httpContext = httpContextAccessor.HttpContext;
             if (httpContext is null)
                 throw new ArgumentNullException(nameof(httpContext));
-            //var a = Thread.CurrentPrincipal as ClaimsPrincipal;
+
             var user = httpContext.User;
-            UserId = user.Claims.FirstOrDefault(d => d.Type == JwtClaimTypes.Subject)?.Value;
-            UserName = user.Claims.FirstOrDefault(d => d.Type == AppClaimTypes.UserDisplayName)?.Value;
+            UserId = user.FindFirstValue(JwtClaimTypes.Subject);
+            UserName = user.FindFirstValue(AppClaimTypes.UserDisplayName);
+            Phone = user.FindFirstValue(JwtClaimTypes.PhoneNumber);
             Roles = user.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray();
-            Phone = user.Claims.FirstOrDefault(d => d.Type == JwtClaimTypes.PhoneNumber)?.Value;
         }
 
         /// <summary>
@@ -40,15 +40,5 @@ namespace Hotline.Api.Token
         /// Roles
         /// </summary>
         public string[] Roles { get; }
-
-        /// <summary>
-        /// Return the first value of the specific <see cref="claimType"/> claim type, otherwise null if the claim is not present.
-        /// </summary>
-        /// <param name="claimType"></param>
-        /// <returns></returns>
-        public string? FindFirstValue(string claimType)
-        {
-            throw new NotImplementedException();
-        }
     }
 }

+ 12 - 16
src/Hotline.Api/Controllers/HomeController.cs

@@ -1,13 +1,9 @@
-using Hotline.Api.Token;
-using Hotline.CallCenter.BlackLists;
-using Hotline.Repository.SqlSugar;
-using Hotline.Share.Requests;
+using Hotline.Repository.SqlSugar;
 using Hotline.Users;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
-using NETCore.Encrypt;
 using SqlSugar;
-using XF.Domain.Exceptions;
+
 namespace Hotline.Api.Controllers;
 
 [AllowAnonymous]
@@ -22,17 +18,17 @@ public class HomeController : BaseController
         _uow = uow;
     }
 
-    [HttpPost("login")]
-    public async Task<string> Login([FromBody] LoginRequest request)
-    {
-        var user =
-            await _userRepository.GetAsync(d => !d.IsDeleted && d.PhoneNo == request.PhoneNo, HttpContext.RequestAborted);
-        if (user is null)
-            throw new UserFriendlyException("未查询到该用户");
+    //[HttpPost("login")]
+    //public async Task<string> Login([FromBody] LoginRequest request)
+    //{
+    //    var user =
+    //        await _userRepository.GetAsync(d => !d.IsDeleted && d.PhoneNo == request.PhoneNo, HttpContext.RequestAborted);
+    //    if (user is null)
+    //        throw new UserFriendlyException("未查询到该用户");
 
-        var token = EncryptProvider.AESEncrypt(System.Text.Json.JsonSerializer.Serialize(user), Sercurity.Key);
-        return token;
-    }
+    //    var token = EncryptProvider.AESEncrypt(System.Text.Json.JsonSerializer.Serialize(user), Sercurity.Key);
+    //    return token;
+    //}
 
     [HttpGet("createdb")]
     public Task CreateDb()

+ 0 - 48
src/Hotline.Api/Filters/TempTokenFilter.cs

@@ -1,48 +0,0 @@
-using System.Security.Claims;
-using Hotline.Api.Token;
-using Hotline.Users;
-using Microsoft.AspNetCore.Mvc.Filters;
-using NETCore.Encrypt;
-using XF.Domain.Exceptions;
-
-namespace Hotline.Api.Filters;
-
-public class TempTokenFilter1 : IAuthorizationFilter
-{
-    /// <summary>
-    /// Called early in the filter pipeline to confirm request is authorized.
-    /// </summary>
-    /// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext" />.</param>
-    public void OnAuthorization(AuthorizationFilterContext context)
-    {
-        if (context.RouteData.Values.Contains(new KeyValuePair<string, object?>("Action", "Login"))) return;
-        if (context.RouteData.Values.Contains(new KeyValuePair<string, object?>("Action", "CreateDb"))) return;
-        if (context.RouteData.Values.Contains(new KeyValuePair<string, object?>("Controller", "Report"))) return;
-        if (context.RouteData.Values.Contains(new KeyValuePair<string, object?>("Controller", "TestSdk"))) return;
-
-        var httpContext = context.HttpContext;
-        var authString = httpContext.Request.Headers["Authorization"].ToString();
-        if (string.IsNullOrEmpty(authString))
-            throw new UserFriendlyException(401, "无效验证信息");
-        var auth = authString.Split("Bearer", StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
-        if (string.IsNullOrEmpty(auth))
-            throw new UserFriendlyException(401, "无效验证信息");
-
-        var userString = EncryptProvider.AESDecrypt(auth, Sercurity.Key);
-        var user = System.Text.Json.JsonSerializer.Deserialize<User>(userString);
-        if (user is null)
-            throw new UserFriendlyException(401, "无效验证信息");
-
-        var contextUser = new ClaimsPrincipal(new List<ClaimsIdentity>
-        {
-            new ClaimsIdentity(new List<Claim>
-            {
-                new Claim("UserId", user.Id),
-                new Claim("UserName", user.Name),
-            })
-        });
-
-        httpContext.User = contextUser;
-    }
-
-}

+ 0 - 1
src/Hotline.Api/Hotline.Api.csproj

@@ -16,7 +16,6 @@
     <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
     <PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="6.0.8" />
-    <PackageReference Include="NETCore.Encrypt" Version="2.1.0" />
     <PackageReference Include="Serilog.Sinks.Exceptionless" Version="3.1.5" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
     <PackageReference Include="XF.Utility.AppIdentityModel" Version="1.0.2" />

+ 0 - 13
src/Hotline.Api/Token/Sercurity.cs

@@ -1,13 +0,0 @@
-namespace Hotline.Api.Token
-{
-    /// <summary>
-    /// 
-    /// </summary>
-    public class Sercurity
-    {
-        /// <summary>
-        /// 
-        /// </summary>
-        public const string Key = "BE439FE4A52F48B9BEAC0602235B0868";
-    }
-}

+ 7 - 0
src/Hotline.Api/appsettings.json

@@ -76,5 +76,12 @@
     "InUse": true,
     "ServerUrl": "http://log.fw.com",
     "ApiKey": "zrSsxjdFC1Uv2WBckZn3RYxjpKKdxfYAN50aFiuV"
+  },
+  "IdentityConfigs": {
+    "IdentityUrl": "http://identity.fengwo.com",
+    "IdentityApiUrl": "http://open.identity.fengwo.com",
+    "ClientId": "hotline_server",
+    "ClientSecret": "ce2fae0e-f0f6-46d6-bd79-1f1a31dff494",
+    "ClientScope": "identity_admin_api"
   }
 }

+ 6 - 6
src/XF.Domain/Authentications/ISessionContext.cs

@@ -24,11 +24,11 @@ public interface ISessionContext
     /// </summary>
     string[] Roles { get; }
 
-    /// <summary>
-    /// Return the first value of the specific <see cref="claimType"/> claim type, otherwise null if the claim is not present.
-    /// </summary>
-    /// <param name="claimType"></param>
-    /// <returns></returns>
-    string? FindFirstValue(string claimType);
+    ///// <summary>
+    ///// Return the first value of the specific <see cref="claimType"/> claim type, otherwise null if the claim is not present.
+    ///// </summary>
+    ///// <param name="claimType"></param>
+    ///// <returns></returns>
+    //string? FindFirstValue(string claimType);
 }