Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# User.Identity.Name在asp.net core+;angular客户端应用程序_C#_Angular_Asp.net Core - Fatal编程技术网

C# User.Identity.Name在asp.net core+;angular客户端应用程序

C# User.Identity.Name在asp.net core+;angular客户端应用程序,c#,angular,asp.net-core,C#,Angular,Asp.net Core,我使用以下.net cli命令: dotnet new angular -o MyAngularApp -au individual 这将产生预期的.net核心Web Api项目,该项目带有嵌入式Angular客户端应用程序,该应用程序具有示例保护Api控制器和示例保护客户端路由 然后我运行应用程序并添加一个用户 我将以下两行添加到默认授权WeatherForecastController get action以调查用户状态: [HttpGet] public IEnumerable<W

我使用以下.net cli命令:

dotnet new angular -o MyAngularApp -au individual
这将产生预期的.net核心Web Api项目,该项目带有嵌入式Angular客户端应用程序,该应用程序具有示例保护Api控制器和示例保护客户端路由

然后我运行应用程序并添加一个用户

我将以下两行添加到默认授权WeatherForecastController get action以调查用户状态:

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
    var isAuthenticated = User.Identity.IsAuthenticated; //1
    var userId = userManager.GetUserId(User); //2
luanchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:62083",
      "sslPort": 44373
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyAngularApp": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

如果发现在.Net Core+Angular base设置中,将填充以下声明并提供用户Id

User.FindFirstValue(ClaimTypes.NameIdentifier)

使用用户id,我们可以获得应用程序用户并执行我们需要的任何操作,在我的情况下,就是在执行特定控制器操作之前验证actionfilter中的角色。

或者,您可以在identity server发出访问令牌时将用户名作为声明包含在内,例如,那么,为什么使用userManager方法会起作用,而使用userManager方法却不行呢?
{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:62083",
      "sslPort": 44373
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyAngularApp": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
User.FindFirstValue(ClaimTypes.NameIdentifier)