配置文档,配置跨域,完善鉴权
This commit is contained in:
parent
2e1b42de48
commit
f3e6328a75
@ -1,11 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Auth0.ManagementApi" Version="7.38.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.17" />
|
||||
@ -21,7 +24,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Middleware\" />
|
||||
<Folder Include="Models\DTOs\" />
|
||||
<Folder Include="Utilities\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
using AGSS.Models;
|
||||
using AGSS.Models.DTOs;
|
||||
using AGSS.Models.Entities;
|
||||
using AGSS.Models.Template;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AGSS.Controllers.User;
|
||||
|
||||
@ -22,13 +24,52 @@ public class UserInfoController : ControllerBase
|
||||
public async Task<IActionResult> My()
|
||||
{
|
||||
|
||||
|
||||
|
||||
// _context.UserModels.Add(user);
|
||||
// await _context.SaveChangesAsync();
|
||||
// return Ok(new ReturnTemplate(200, "成功",user));
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新或创建用户信息。
|
||||
/// </summary>
|
||||
/// <param name="data">包含用户详细信息的UserRequest对象,包括性别、生日、描述、职业名称和职业代码。</param>
|
||||
/// <returns>一个IActionResult,表示操作的成功或失败。如果成功,则返回200状态码以及消息和用户信息。用户信息封装在ReturnTemplate中,包含一个成功代码、一条消息和数据。</returns>
|
||||
/// <remarks>此方法需要授权,并接受POST请求。它使用从认证令牌中提取的AuthId来创建或更新数据库中的用户信息。</remarks>
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> NextInfo([FromBody]UserRequest data)
|
||||
{
|
||||
var authId = this.User.FindFirst("sub")!.Value;
|
||||
UserModel user = new UserModel
|
||||
{
|
||||
AuthId = "123"
|
||||
AuthId = authId,
|
||||
Sex = data.Sex,
|
||||
Birthday = data.Birthday,
|
||||
Description = data.Description,
|
||||
JobName = data.JobName,
|
||||
JobCode = data.JobCode
|
||||
};
|
||||
|
||||
_context.UserModels.Add(user);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok(new ReturnTemplate(200, "成功",user));
|
||||
return Ok(new ReturnTemplate(200, "成功",user));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查当前认证用户是否为新用户。
|
||||
/// </summary>
|
||||
/// <returns>一个IActionResult,表示操作的成功或失败。如果成功,则返回200状态码以及消息和isNewUser布尔值。该布尔值封装在ReturnTemplate中,包含一个成功代码、一条消息和数据。</returns>
|
||||
/// <remarks>此方法需要授权,并接受GET请求。它通过检查数据库中是否存在具有相同AuthId的用户来确定用户是否为新用户。</remarks>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> IsNewUser()
|
||||
{
|
||||
var authId = this.User.FindFirst("sub")!.Value;
|
||||
var isNewUser=await _context.UserModels.AnyAsync(a => a.AuthId == authId);
|
||||
return Ok(new ReturnTemplate(200, "成功",isNewUser));
|
||||
}
|
||||
}
|
42
AGSS/Models/DTOs/UserRequest.cs
Normal file
42
AGSS/Models/DTOs/UserRequest.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AGSS.Models.DTOs;
|
||||
|
||||
public class UserRequest
|
||||
{
|
||||
[MaxLength(20)]
|
||||
public string? Sex { get; set; }
|
||||
[MaxLength(100)]
|
||||
public string? Description { get; set; }
|
||||
[MaxLength(200)]
|
||||
public string? Config { get; set; }
|
||||
[MaxLength(10)]
|
||||
public string? JobCode { get; set; }
|
||||
[MaxLength(10)]
|
||||
public string? JobName { get; set; }
|
||||
[MaxLength(20)]
|
||||
public string? Birthday { get; set; }
|
||||
|
||||
}
|
||||
public class UserReturn
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
[MaxLength(50)]
|
||||
public required string AuthId { get; set; }
|
||||
[MaxLength(20)]
|
||||
public string? Sex { get; set; }
|
||||
[MaxLength(100)]
|
||||
public string? Description { get; set; }
|
||||
[MaxLength(200)]
|
||||
public string? Config { get; set; }
|
||||
[MaxLength(10)]
|
||||
public string? JobCode { get; set; }
|
||||
[MaxLength(10)]
|
||||
public string? JobName { get; set; }
|
||||
[MaxLength(20)]
|
||||
public string? Birthday { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string NickName { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
}
|
@ -1,8 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AGSS.Models.Entities;
|
||||
|
||||
public class UserModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
[MaxLength(50)]
|
||||
public required string AuthId { get; set; }
|
||||
[MaxLength(20)]
|
||||
public string? Sex { get; set; }
|
||||
[MaxLength(100)]
|
||||
public string? Description { get; set; }
|
||||
[MaxLength(200)]
|
||||
public string? Config { get; set; }
|
||||
[MaxLength(10)]
|
||||
public string? JobCode { get; set; }
|
||||
[MaxLength(10)]
|
||||
public string? JobName { get; set; }
|
||||
[MaxLength(20)]
|
||||
public string? Birthday { get; set; }
|
||||
|
||||
}
|
@ -1,17 +1,10 @@
|
||||
using System.Reflection;
|
||||
using AGSS.Models;
|
||||
using AGSS.Models.Template;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@ -22,6 +15,17 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
var domain = builder.Configuration["Auth0:Domain"];
|
||||
var audience =builder.Configuration["Auth0:Audience"];
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAll", builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin() // 允许所有来源
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
//数据库配置(PGSQL)
|
||||
builder.Services.AddDbContext<DBContext>(opt =>
|
||||
opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext")));
|
||||
@ -59,16 +63,21 @@ builder.Services.AddAuthorization();
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1",new OpenApiInfo{Title = "ZeroNode后端文档", Version = "1.0.0",Description = "使用了Dotnet9.0,数据库采用PGSql,作者:罗澜,7000"});
|
||||
var xmlFil = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFil);
|
||||
c.IncludeXmlComments(xmlPath);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
// 配置Swagger
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
@ -88,7 +97,7 @@ app.Use(async (context, next) =>
|
||||
context.Response.ContentType = "application/json";
|
||||
|
||||
// 自定义响应内容
|
||||
await context.Response.WriteAsJsonAsync(new ReturnTemplate(404,"未能找到资源吖!",null));
|
||||
await context.Response.WriteAsJsonAsync(new ReturnTemplate(404,"未能找到资源吖!",null!));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -7,11 +7,11 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Auth0": {
|
||||
"Domain": "",
|
||||
"Audience": "",
|
||||
"ClientId": ""
|
||||
"Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/",
|
||||
"Audience": "https://zeronode.AGSBackend.com",
|
||||
"ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p"
|
||||
},
|
||||
"DataBase": {
|
||||
"ConnectionString": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
||||
"ConnectionStrings": {
|
||||
"DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,9 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Auth0": {
|
||||
"Domain": "",
|
||||
"Audience": "",
|
||||
"ClientId": ""
|
||||
"Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/",
|
||||
"Audience": "https://zeronode.AGSBackend.com",
|
||||
"ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
||||
|
7
global.json
Normal file
7
global.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "9.0.0",
|
||||
"rollForward": "latestMajor",
|
||||
"allowPrerelease": true
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user