This commit is contained in:
杨毅 2025-01-27 00:16:06 +08:00
parent 73f2a80b2c
commit 971d011469
4 changed files with 38 additions and 5 deletions

View File

@ -90,6 +90,7 @@ namespace asg_form.Controllers
public async Task<IActionResult> LoginCallback( public async Task<IActionResult> LoginCallback(
string type, string type,
[FromServices] GithubOAuth githubOAuth, [FromServices] GithubOAuth githubOAuth,
[FromServices] MicrosoftOAuth microsoftOAuth,
[FromServices] IOptions<JWTOptions> jwtOptions, [FromServices] IOptions<JWTOptions> jwtOptions,
[FromQuery] string code, [FromQuery] string code,
[FromQuery] string state) [FromQuery] string state)
@ -124,7 +125,35 @@ namespace asg_form.Controllers
} }
string jwtToken = BuildToken(claims, jwtOptions.Value); string jwtToken = BuildToken(claims, jwtOptions.Value);
return Redirect($"https://commentary.idvasg.cn/oauth/loginok?token={jwtToken}"); return Redirect($"https://commentary.idvasg.cn/oauth/loginok?token={jwtToken}");
break;
}
case "microsoft":
{
var authorizeResult = await microsoftOAuth.AuthorizeCallback(code, state);
if (!authorizeResult.IsSccess)
{
throw new Exception(authorizeResult.ErrorMessage);
}
var userinfo = await githubOAuth.GetUserInfoAsync(authorizeResult.AccessToken);
var user = new User() { Id = 20, UserName = $"ms{userinfo.Name}", Email = userinfo.Email };
if (user == null)
{
var user_new = await userManager.FindByNameAsync(userinfo.Name);
var r = await userManager.CreateAsync(user);
return Redirect($"https://commentary.idvasg.cn/oauth/next?username=gh{userinfo.Name}");
}
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
claims.Add(new Claim(ClaimTypes.Name, user.UserName));
var roles = await userManager.GetRolesAsync(user);
foreach (string role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}
string jwtToken = BuildToken(claims, jwtOptions.Value);
return Redirect($"https://commentary.idvasg.cn/oauth/loginok?token={jwtToken}");
break;
} }
default: default:
throw new Exception($"没有实现【{type}】登录回调!"); throw new Exception($"没有实现【{type}】登录回调!");

View File

@ -5,6 +5,7 @@ using Flandre.Adapters.OneBot.Extensions;
using Flandre.Framework; using Flandre.Framework;
using IGeekFan.AspNetCore.Knife4jUI; using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -19,6 +20,7 @@ using Mirai.Net.Sessions.Http.Managers;
using MrHuo.OAuth; using MrHuo.OAuth;
using MrHuo.OAuth.Github; using MrHuo.OAuth.Github;
using MrHuo.OAuth.Microsoft; using MrHuo.OAuth.Microsoft;
using Scalar.AspNetCore;
using System; using System;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
@ -157,8 +159,8 @@ var app = builder.Build();
app.UseCors(); app.UseCors();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
app.UseSwagger(); app.MapScalarApiReference(); // scalar/v1
app.UseSwaggerUI(); app.MapOpenApi();
app.UseKnife4UI(c => app.UseKnife4UI(c =>
{ {
c.RoutePrefix = string.Empty; c.RoutePrefix = string.Empty;

View File

@ -53,9 +53,9 @@
"scope": "repo" "scope": "repo"
}, },
"microsoft": { "microsoft": {
"client_id": "9206e9e3-4608-4501-85ae-88b42c7f2fe1", "app_id": "9206e9e3-4608-4501-85ae-88b42c7f2fe1",
"tenant_id": "7ed846b8-c314-431e-b72c-27562dc7bf04", "tenant_id": "7ed846b8-c314-431e-b72c-27562dc7bf04",
"client_secret": "E5O8Q~OPLX3AjKwQz.5hlo7SUDoIdOaurL6F8cWl", "app_key": "E5O8Q~OPLX3AjKwQz.5hlo7SUDoIdOaurL6F8cWl",
"redirect_uri": "https://api.idvasg.cn/oauth/microsoftcallback", "redirect_uri": "https://api.idvasg.cn/oauth/microsoftcallback",
"scope": "snsapi_userinfo" "scope": "snsapi_userinfo"
}, },

View File

@ -45,6 +45,7 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" /> <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Mirai.Net" Version="2.5.1" /> <PackageReference Include="Mirai.Net" Version="2.5.1" />
<PackageReference Include="MrHuo.OAuth.Core" Version="1.1.1" /> <PackageReference Include="MrHuo.OAuth.Core" Version="1.1.1" />
@ -53,6 +54,7 @@
<PackageReference Include="NLog" Version="5.2.4" /> <PackageReference Include="NLog" Version="5.2.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="RestSharp" Version="106.12.0" /> <PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="Scalar.AspNetCore" Version="2.0.4" />
<PackageReference Include="SkiaSharp" Version="2.88.8" /> <PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.0" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.0" />