Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Authentication ASP.NET核心身份验证:同一控制器中某个操作的承载令牌和另一个操作的Windows身份验证_Authentication_Asp.net Core - Fatal编程技术网

Authentication ASP.NET核心身份验证:同一控制器中某个操作的承载令牌和另一个操作的Windows身份验证

Authentication ASP.NET核心身份验证:同一控制器中某个操作的承载令牌和另一个操作的Windows身份验证,authentication,asp.net-core,Authentication,Asp.net Core,我想做的是在API中提出不同类型的端点,这些端点可以通过Windows身份验证或JWT承载身份验证进行访问 在Startup.cs-->Configure中,身份验证按如下方式配置,以允许使用所需参数进行承载身份验证: 为了保护我的端点,我尝试了以下方法: 它正在发挥作用,但我的问题是: 这似乎合乎逻辑吗?我的目标是保护交付用户令牌的端点。一个端点将使用JWT令牌(刷新令牌)和特定角色进行保护,一个端点将使用Windows凭据进行保护 我错过什么了吗?当我只使用Windows身份验证时,我使用

我想做的是在API中提出不同类型的端点,这些端点可以通过Windows身份验证或JWT承载身份验证进行访问

Startup.cs-->Configure中,身份验证按如下方式配置,以允许使用所需参数进行承载身份验证: 为了保护我的端点,我尝试了以下方法: 它正在发挥作用,但我的问题是:
  • 这似乎合乎逻辑吗?我的目标是保护交付用户令牌的端点。一个端点将使用JWT令牌(刷新令牌)和特定角色进行保护,一个端点将使用Windows凭据进行保护
  • 我错过什么了吗?当我只使用Windows身份验证时,我使用设置services.AddAuthentication(IISDefaults.AuthenticationScheme)在我的Startup.cs-->Configure中,似乎没有必要在上述实现中工作,但我真的不知道这行的作用是什么,以及是否有必要(顺便说一句,似乎没有)
  • 是否有更聪明/更漂亮的方法来检查用户类型?也许有点像自定义属性
谢谢你的建议

看来如果没有必要工作

这是因为
WebHost.CreateDefaultBuilder(args)
为您完成了这项工作。该方法将调用
.UseIISIntegration()并在后台添加相关服务。有关详细信息,请参阅
和

是否有更聪明/更漂亮的方法来检查用户类型?也许有点像自定义属性

您不必在操作方法中手动检查
if(user.GetType()==typeof(System.Security.Principal.WindowsPrincipal){return ok;}return Unauthorized()
。请改用内置的
authorized属性

[Authorize(AuthenticationSchemes = "Windows")] [HttpGet("[action]")] public IActionResult AuthorizeWindowsUser() { var user = HttpContext.User; if (user.GetType() == typeof(System.Security.Principal.WindowsPrincipal)) { return Ok(); } return Unauthorized(); return Ok(); // only users who are authorized with the Windows scheme can access this method } 哇,[Authorize(AuthenticationSchemes=“Windows”)]正是我所需要的,非常感谢您的回答和相关解释,非常有帮助。
// THIS WAY I CAN LIMIT TO WINDOWS CREDENTIAL
[Authorize]
[HttpGet("[action]")]
public IActionResult AuthorizeWindowsUser()
{
    var user = HttpContext.User;
    if (user.GetType() == typeof(System.Security.Principal.WindowsPrincipal))
    {
        return Ok();
    }
    return Unauthorized();
}

// THIS WAY I CAN LIMIT TO JWT
[Authorize]
[HttpGet("[action]")]
public IActionResult AuthorizeLoginUser()
{
    var user = HttpContext.User;
    if (user.GetType() == typeof(System.Security.Claims.ClaimsPrincipal))
    {
        return Ok();
    }
    return Unauthorized();
}
[Authorize(AuthenticationSchemes = "Windows")] [HttpGet("[action]")] public IActionResult AuthorizeWindowsUser() { var user = HttpContext.User; if (user.GetType() == typeof(System.Security.Principal.WindowsPrincipal)) { return Ok(); } return Unauthorized(); return Ok(); // only users who are authorized with the Windows scheme can access this method } [Authorize] [HttpGet("[action]")] public IActionResult AuthorizeLoginUser() { var user = HttpContext.User; if (user.GetType() == typeof(System.Security.Claims.ClaimsPrincipal)) { return Ok(); } return Unauthorized(); return Ok(); // only users who are authorized with the default scheme can access this method }