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