xf il y a 1 an
Parent
commit
e65ed24054

+ 13 - 2
src/Identity.Host/Apis/TestController.cs

@@ -4,13 +4,13 @@ using XF.Utility.UnifyResponse;
 
 namespace Identity.Host.Apis
 {
-    [AllowAnonymous]
     [ApiController]
     [Produces("application/json")]
     [Route("api/v1/[controller]")]
 
     public class TestController : ControllerBase
     {
+        [Authorize]
         [HttpGet("time")]
         public async Task<ApiResponse<string>> Time()
         {
@@ -21,7 +21,18 @@ namespace Identity.Host.Apis
             };
         }
 
-        [HttpPost("time1")]
+        [AllowAnonymous]
+        [HttpGet("time1")]
+        public async Task<ApiResponse<string>> Time1()
+        {
+            var data = DateTime.Now.ToString("F");
+            return new ApiResponse<string>
+            {
+                Result = data
+            };
+        }
+
+        [HttpPost("time")]
         public async Task<ApiResponse<string>> Time(int t)
         {
             var data = DateTime.Now.ToString("F");

+ 3 - 1
src/Identity.Host/Identity.Host.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk.Web">
+<Project Sdk="Microsoft.NET.Sdk.Web">
 
   <PropertyGroup>
     <TargetFramework>net7.0</TargetFramework>
@@ -8,6 +8,8 @@
 
   <ItemGroup>
     <PackageReference Include="IdentityServer4" Version="4.1.2" />
+    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.9" />
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
   </ItemGroup>
 
   <ItemGroup>

+ 28 - 2
src/Identity.Host/Program.cs

@@ -1,4 +1,7 @@
+using System.IdentityModel.Tokens.Jwt;
 using Identity.Host;
+using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.IdentityModel.Tokens;
 
 var builder = WebApplication.CreateBuilder(args);
 var services = builder.Services;
@@ -7,8 +10,23 @@ var services = builder.Services;
 builder.Services.AddControllersWithViews();
 
 services
-    .RegisterIds4()
-    .AddAuthentication();
+    .RegisterIds4();
+//.AddAuthentication();
+
+JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
+services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
+    .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, d =>
+    {
+        d.Authority = "http://localhost:50200";
+        d.RequireHttpsMetadata = false;
+        d.TokenValidationParameters = new TokenValidationParameters
+        {
+            ValidateAudience = false
+        };
+        //d.Audience = "hotline_api";
+    });
+
+services.AddSwaggerGen();
 
 var app = builder.Build();
 
@@ -17,12 +35,20 @@ if (!app.Environment.IsDevelopment())
 {
     app.UseExceptionHandler("/Home/Error");
 }
+
+//if (app.Environment.IsDevelopment())
+//{
+app.UseSwagger();
+app.UseSwaggerUI();
+//}
+
 app.UseStaticFiles();
 
 app.UseRouting();
 
 app.UseIdentityServer();
 
+app.UseAuthentication();
 app.UseAuthorization();
 
 app.MapControllerRoute(