linux - Identity Server 4 - Linux 上的用户声明为空

标签 linux identityserver4 asp.net-core-2.0

问题:

在 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/

相关文章:

c# - ASN.NET Core 2.0 Facebook 身份验证 ExternalLoginSignInAsync 失败(IsNotAllowed)

c# - ASP.NET Core 2 razor 页面中的文件上传

linux - 以 root 权限运行 nmap 时出错 - Linux

php - 如何在 CentOS 6.2 上安装 PHP mbstring

linux - 远程搜索文件中的文本

c# - 使用数据库而不是内存存储身份服务器 4

adfs - 带有 ADFS 4.0 的 Identityserver4,无法获取用户信息或声明

asp.net-mvc - 不使用身份的外部登录 asp.net core 2.0

android - ffmpeg 构建错误看起来 tmp 文件创建失败?

.net-core - 是否有一个示例说明如何创建使用 IdentityServer4 进行身份验证的 dotnet core 3 控制台应用程序?