diff --git a/asg_form/Controllers/OAuthController.cs b/asg_form/Controllers/OAuthController.cs index a2e295c..4e4f521 100644 --- a/asg_form/Controllers/OAuthController.cs +++ b/asg_form/Controllers/OAuthController.cs @@ -90,6 +90,7 @@ namespace asg_form.Controllers public async Task LoginCallback( string type, [FromServices] GithubOAuth githubOAuth, + [FromServices] MicrosoftOAuth microsoftOAuth, [FromServices] IOptions jwtOptions, [FromQuery] string code, [FromQuery] string state) @@ -124,7 +125,35 @@ namespace asg_form.Controllers } string jwtToken = BuildToken(claims, jwtOptions.Value); 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(); + 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: throw new Exception($"没有实现【{type}】登录回调!"); diff --git a/asg_form/Program.cs b/asg_form/Program.cs index 48dcd49..0dd75db 100644 --- a/asg_form/Program.cs +++ b/asg_form/Program.cs @@ -5,6 +5,7 @@ using Flandre.Adapters.OneBot.Extensions; using Flandre.Framework; using IGeekFan.AspNetCore.Knife4jUI; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -19,6 +20,7 @@ using Mirai.Net.Sessions.Http.Managers; using MrHuo.OAuth; using MrHuo.OAuth.Github; using MrHuo.OAuth.Microsoft; +using Scalar.AspNetCore; using System; using System.Data; using System.Drawing; @@ -157,8 +159,8 @@ var app = builder.Build(); app.UseCors(); // Configure the HTTP request pipeline. -app.UseSwagger(); - app.UseSwaggerUI(); +app.MapScalarApiReference(); // scalar/v1 +app.MapOpenApi(); app.UseKnife4UI(c => { c.RoutePrefix = string.Empty; diff --git a/asg_form/appsettings.json b/asg_form/appsettings.json index cbf6873..490c2c7 100644 --- a/asg_form/appsettings.json +++ b/asg_form/appsettings.json @@ -53,9 +53,9 @@ "scope": "repo" }, "microsoft": { - "client_id": "9206e9e3-4608-4501-85ae-88b42c7f2fe1", + "app_id": "9206e9e3-4608-4501-85ae-88b42c7f2fe1", "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", "scope": "snsapi_userinfo" }, diff --git a/asg_form/asg_form.csproj b/asg_form/asg_form.csproj index c27c89b..b570a3c 100644 --- a/asg_form/asg_form.csproj +++ b/asg_form/asg_form.csproj @@ -45,6 +45,7 @@ + @@ -53,6 +54,7 @@ +