问题:
在 Linux 服务器上,用户声明不会从 Auth 传递到 API。在 Windows 机器上一切正常(在 VS2017 下并使用 CMD)
我有一个具有特殊授权属性的 Controller :
[Authorize(Policy = "identity")]
[Route("routes")]
public class RoutesController : Controller
{
[HttpGet]
public IActionResult Get(string postId, string routeType)
{
//
}
在 Startup.cs 中,我提供了正确的条目:
services.AddMvc(options => {
options.Filters.Add(new AuthorizeFilter("identity"));
});
services.AddAuthorization(options => {
options.AddPolicy("identity", policy =>
{
policy.Requirements.Add(new IdentityUserRequirement());
});
});
services.AddSingleton<IAuthorizationHandler, IdentityUserHandler>();
并在/Filters 目录中创建适当的类:
public class IdentityUserHandler : AuthorizationHandler<IdentityUserRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, IdentityUserRequirement requirement)
{
UserModel user = _userRepository.FindById(context.User.Claims.SingleOrDefault(x => x.Type == "userid")?.Value);
user.IMEI = context.User.Claims.SingleOrDefault(x => x.Type == "imei")?.Value;
_userRepository.UpdateUser(user);
context.Succeed(requirement);
return Task.FromResult(0);
}
}
我使用 Postman 调用 Auth 服务来获取 access_token,然后使用该 token 调用我的 RoutesController Get 方法。
在 Windows 上(在 VS2015 下启动服务或通过 CMD 启动服务)方法返回带有路由的普通 JSON。
当我在 Linux 上部署解决方案时出现以下错误:
NullReferenceException: Object reference not set to an instance of an object.
并且将我的 IdentityUserHandler 类方法 HandleRequirementAsync 引用到行:
user.IMEI = context.User.Claims.SingleOrDefault(x => x.Type == "imei")?.Value;
据我调试 Linux 环境的经理看来,问题在于:
AuthorizationHandlerContext context
方法 HandleRequirementAsync 中的参数
集合 contex.User.Claims 没有元素。在 Windows 上有,在 Linux 上没有。
谁知道为什么?
最佳答案
我做了一些测试,看起来这只适用于我的开发人员机器,在:
- Centos
- Win 服务器 2k8
- 赢得服务器 2k16
存在问题
关于linux - Identity Server 4 - Linux 上的用户声明为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47097493/