Files
yunhai.netcore.tool/BasePlatformDotnet/samples/Yh.Platform.Sample.Web/Program.cs
T
2026-09-02 09:38:45 +08:00

55 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Yh.Platform.Abstractions;
using Yh.Platform.Auth;
using Yh.Platform.OpenApi;
var builder = WebApplication.CreateBuilder(args);
// 一次带齐:/oapi 客户端 + 应用签名 + Cookie/CAS 认证 + 用户上下文
builder.Services.AddYhPlatform(builder.Configuration);
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseYhUserContext();
// 平台验收载体:CAS 登录 → BoaUser 上下文注入
app.MapGet("/", () => Results.Text(
"yh-platform-net sample。\n" +
"GET /me —— CAS 登录后返回 BoaUser(含 currentRole)\n" +
"GET /user/{id} —— 签名调 UserBase /oapi/user/byId\n" +
"GET /roles/{id} —— 签名调 /oapi/user/roles"));
app.MapGet("/me", (HttpContext ctx) =>
{
var user = BoaUserContext.Get();
if (user == null)
{
// 未登录 → challenge CAS
return Results.Challenge(new AuthenticationProperties { RedirectUri = "/me" }, new[] { YhAuthDefaults.CasScheme });
}
return Results.Ok(new
{
user.Id,
user.Account,
user.Name,
School = user.School?.Name,
CurrentRole = user.CurrentRole,
Roles = user.RoleIds,
});
});
// 签名调 /oapi:byId / byLoginName / byPhone 三入口
app.MapGet("/user/{id:int}", async (int id, IOpenApiUserClient client) => Results.Ok(await client.GetUserInfoAsync(id)));
app.MapGet("/roles/{id:int}", async (int id, IOpenApiUserClient client) => Results.Ok(await client.GetUserRoleListAsync(id)));
app.MapGet("/user/by-login-name/{loginName}", async (string loginName, IOpenApiUserClient client) => Results.Ok(await client.GetUserInfoAsync(loginName)));
// 静态门面等价性演示
app.MapGet("/facade/{id:int}", async (int id) => Results.Ok(await YschBaseOpenApi.GetUserInfoAsync(id)));
app.Run();