更新Auth0配置并优化JWT验证和用户控制器
This commit is contained in:
parent
f3e6328a75
commit
68ac3a2ca2
@ -1,3 +1,4 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
using AGSS.Models;
|
using AGSS.Models;
|
||||||
using AGSS.Models.DTOs;
|
using AGSS.Models.DTOs;
|
||||||
using AGSS.Models.Entities;
|
using AGSS.Models.Entities;
|
||||||
@ -24,11 +25,6 @@ public class UserInfoController : ControllerBase
|
|||||||
public async Task<IActionResult> My()
|
public async Task<IActionResult> My()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// _context.UserModels.Add(user);
|
|
||||||
// await _context.SaveChangesAsync();
|
|
||||||
// return Ok(new ReturnTemplate(200, "成功",user));
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +39,7 @@ public class UserInfoController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<IActionResult> NextInfo([FromBody]UserRequest data)
|
public async Task<IActionResult> NextInfo([FromBody]UserRequest data)
|
||||||
{
|
{
|
||||||
var authId = this.User.FindFirst("sub")!.Value;
|
var authId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
UserModel user = new UserModel
|
UserModel user = new UserModel
|
||||||
{
|
{
|
||||||
AuthId = authId,
|
AuthId = authId,
|
||||||
@ -68,7 +64,7 @@ public class UserInfoController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<IActionResult> IsNewUser()
|
public async Task<IActionResult> IsNewUser()
|
||||||
{
|
{
|
||||||
var authId = this.User.FindFirst("sub")!.Value;
|
var authId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
var isNewUser=await _context.UserModels.AnyAsync(a => a.AuthId == authId);
|
var isNewUser=await _context.UserModels.AnyAsync(a => a.AuthId == authId);
|
||||||
return Ok(new ReturnTemplate(200, "成功",isNewUser));
|
return Ok(new ReturnTemplate(200, "成功",isNewUser));
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
using AGSS.Models;
|
using AGSS.Models;
|
||||||
using AGSS.Models.Template;
|
using AGSS.Models.Template;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
@ -36,13 +37,21 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|||||||
{
|
{
|
||||||
options.Authority = domain;
|
options.Authority = domain;
|
||||||
options.Audience = audience;
|
options.Audience = audience;
|
||||||
|
options.RequireHttpsMetadata = false;
|
||||||
options.TokenValidationParameters = new TokenValidationParameters
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
ValidateIssuer = true,
|
ValidateIssuer = true,
|
||||||
ValidateAudience = true,
|
ValidateAudience = true,
|
||||||
ValidateLifetime = true,
|
ValidateLifetime = true,
|
||||||
ValidIssuer = domain,
|
ValidIssuer = domain,
|
||||||
ValidAudience = audience
|
|
||||||
|
|
||||||
|
// ValidAudience = audience,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
options.Events = new JwtBearerEvents
|
options.Events = new JwtBearerEvents
|
||||||
{
|
{
|
||||||
@ -69,6 +78,29 @@ builder.Services.AddSwaggerGen(c =>
|
|||||||
var xmlFil = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
var xmlFil = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||||
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFil);
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFil);
|
||||||
c.IncludeXmlComments(xmlPath);
|
c.IncludeXmlComments(xmlPath);
|
||||||
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
In = ParameterLocation.Header,
|
||||||
|
Type = SecuritySchemeType.ApiKey,
|
||||||
|
Description = "直接在下框中输入Bearer {token}(注意两者之间是一个空格)",
|
||||||
|
Name = "Authorization",
|
||||||
|
BearerFormat = "JWT",
|
||||||
|
Scheme = "Bearer"
|
||||||
|
});
|
||||||
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Reference=new OpenApiReference
|
||||||
|
{
|
||||||
|
Type=ReferenceType.SecurityScheme,
|
||||||
|
Id="Bearer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new string[] {}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"Auth0": {
|
"Auth0": {
|
||||||
"Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/",
|
"Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/",
|
||||||
"Audience": "https://zeronode.AGSBackend.com",
|
"Audience": "https://AGSSadmin.ASGG.com",
|
||||||
"ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p"
|
"ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p",
|
||||||
|
"Secret": "7wU9bdVfBsX3jITh0w4bgE6fkvLk8pIcZRSUw6r8HQUnXfslYxlx4c4E0ZAIw4Ak"
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
"DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"Auth0": {
|
"Auth0": {
|
||||||
"Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/",
|
"Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/",
|
||||||
"Audience": "https://zeronode.AGSBackend.com",
|
"Audience": "https://AGSSadmin.ASGG.com",
|
||||||
"ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p"
|
"ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p",
|
||||||
|
"Secret": "7wU9bdVfBsX3jITh0w4bgE6fkvLk8pIcZRSUw6r8HQUnXfslYxlx4c4E0ZAIw4Ak"
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
"DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user