test: 测试qq回调

This commit is contained in:
橙子
2024-01-05 23:05:13 +08:00
parent 310e6749b8
commit 093b5a7a7b
4 changed files with 67 additions and 26 deletions

View File

@@ -70,7 +70,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Bbs.SqlSugarCo
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "audit-logging", "audit-logging", "{73CCF2C4-B9FD-44AB-8D4B-0A421805B094}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "audit-logging", "audit-logging", "{73CCF2C4-B9FD-44AB-8D4B-0A421805B094}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.AuditLogging.SqlSugarCore", "module\audit-logging\Yi.AuditLogging.SqlSugarCore\Yi.AuditLogging.SqlSugarCore.csproj", "{48806510-8E18-4E1E-9BAF-5B97E88C5FC3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.AuditLogging.SqlSugarCore", "module\audit-logging\Yi.AuditLogging.SqlSugarCore\Yi.AuditLogging.SqlSugarCore.csproj", "{48806510-8E18-4E1E-9BAF-5B97E88C5FC3}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Quartz.Logging;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
namespace Yi.Framework.Bbs.Application.Services.Authentication
{
public class QQAuthService : IRemoteService, ITransientDependency
{
private HttpContext HttpContext { get; set; }
private ILogger<QQAuthService> _logger;
public QQAuthService(IHttpContextAccessor httpContextAccessor, ILogger<QQAuthService> logger)
{
_logger = logger;
HttpContext = httpContextAccessor.HttpContext ?? throw new ApplicationException("未注册Http");
}
[HttpGet("/auth/qq")]
public async Task AuthQQAsync()
{
var data = await HttpContext.AuthenticateAsync("QQ");
_logger.LogError($"QQ回调信息:{Newtonsoft.Json.JsonConvert.SerializeObject(data)}");
_logger.LogError($"QQ回调身份:{Newtonsoft.Json.JsonConvert.SerializeObject(data.Principal)}");
}
}
}

View File

@@ -4,6 +4,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.QQ" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" /> <PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />

View File

@@ -66,7 +66,7 @@ namespace Yi.Abp.Web
service.AddControllers().AddNewtonsoftJson(options => service.AddControllers().AddNewtonsoftJson(options =>
{ {
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
options.SerializerSettings.Converters.Add(new StringEnumConverter()); options.SerializerSettings.Converters.Add(new StringEnumConverter());
}); });
Configure<AbpAntiForgeryOptions>(options => Configure<AbpAntiForgeryOptions>(options =>
@@ -103,33 +103,40 @@ namespace Yi.Abp.Web
//jwt鉴权 //jwt鉴权
var jwtOptions = configuration.GetSection(nameof(JwtOptions)).Get<JwtOptions>(); var jwtOptions = configuration.GetSection(nameof(JwtOptions)).Get<JwtOptions>();
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => .AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{ {
options.TokenValidationParameters = new TokenValidationParameters ClockSkew = TimeSpan.Zero,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtOptions.Issuer,
ValidAudience = jwtOptions.Audience,
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecurityKey))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{ {
ClockSkew = TimeSpan.Zero, var accessToken = context.Request.Query["access_token"];
ValidateIssuer = true, if (!string.IsNullOrEmpty(accessToken))
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtOptions.Issuer,
ValidAudience = jwtOptions.Audience,
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecurityKey))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{ {
var accessToken = context.Request.Query["access_token"]; context.Token = accessToken;
if (!string.IsNullOrEmpty(accessToken))
{
context.Token = accessToken;
}
return Task.CompletedTask;
} }
}; return Task.CompletedTask;
}); }
};
})
.AddQQ("QQ", options =>
{
options.ClientId = configuration["qq:client"];
options.ClientSecret = configuration["qq:key"];
options.CallbackPath = "/qq";
});
//授权 //授权
context.Services.AddAuthorization(); context.Services.AddAuthorization();
@@ -153,7 +160,7 @@ namespace Yi.Abp.Web
//swagger //swagger
app.UseYiSwagger(); app.UseYiSwagger();
//请求处理 //请求处理
app.UseYiApiHandlinge(); app.UseYiApiHandlinge();