asp.net-core - IHttpContextAccessor 在 AspNetCore 5 API 控制器中返回空用户名声明
问题描述
我的 AspNetCore 5 Web 应用程序中有一些 Api 控制器,其中包含一些可供未经身份验证的用户使用的方法,其中一些仅对授权用户可用。
假设有一个所有用户都可以添加评论的 ReviewController,但是如果用户通过了身份验证,我们应该填写用户名字段。
让我们在TestController中说:
[Route("api/[controller]/[action]/")]
[ApiController]
public class TestController
{
private readonly IHttpContextAccessor _httpContextAccessor;
public TestController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
[HttpGet]
public string TestA()
{
return _httpContextAccessor.HttpContext.User.Identity.Name;
}
[HttpGet]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public string TestB()
{
return _httpContextAccessor.HttpContext.User.Identity.Name;
}
}
两种方法之间的唯一区别是Authorize
属性。
我使用以下设置在应用程序中设置JwtBearer身份验证。
配置服务:
public override void ConfigureServices(IServiceCollection services)
{
var jwtConfig = new JwtConfig
{
Secret = "some secrets!",
Audience = "http://localhost:5000",
Expires = 600,
Issuer = "http://localhost:5000"
};
services.AddAuthorization();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwt =>
{
var key = Encoding.ASCII.GetBytes(jwtConfig.Secret);
jwt.SaveToken = true;
jwt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = true,
ValidateAudience = true,
RequireExpirationTime = true,
ValidateLifetime = true,
SaveSigninToken = true,
ValidAudience = jwtConfig.Audience,
ValidIssuer = jwtConfig.Issuer
};
});
services.AddHttpContextAccessor();
services.AddMvcCore()
.AddDataAnnotations()
.AddApiExplorer()
.AddFormatterMappings()
.AddCors();
}
并配置:
public void Configure(IApplicationBuilder app)
{
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
当我调用TestB
它时,它会返回登录的用户名。但是,当我调用它时,TestA
它返回 null。
为什么IHttpContextAccessor
没有加载用户声明以及如何IHttpContextAccessor
在任何情况下访问用户名和用户声明?假设我在控制器以外的其他层中使用它。
解决方案
推荐阅读
- python - 循环将数据帧逐块转换为 np 数组
- flutter - 在flutter中,如何在ListView的底部和顶部添加一个按钮
- google-apps-script - 使用谷歌应用脚本查找两次之间的差异;了解 Logger.log 响应
- html - 如何使用 TWIG 计算课程出现在页面上的次数?
- vue.js - Facebook/Twitter 上的 Nuxt.js 预览不起作用:404 错误
- javascript - 使用类组件在 React 中将 Ref 从子级传递给父级
- java - 循环内的 PreparedStatement.setString 是如何工作的?
- python - 如何让 TensorFlow 安装在我的计算机上?点安装不工作
- react-native - React-native FlatList 组件不工作
- ruby-on-rails - Typhoeus 是线程安全的吗?