ASP.NET中能否在同一应用程序中使用Windows身份验证和JWT?
创始人
2024-09-19 21:31:03
0

是的,可以在同一应用程序中同时使用Windows身份验证和JWT。以下是如何实现的简要步骤:

  1. 在Web.config中启用Windows身份验证

  1. 为需要使用JWT的控制器添加Authorize特性
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ProtectedController : ControllerBase
{
    // Your protected controller methods
}
  1. 在Startup.cs中配置JWT身份验证
services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidIssuer = "example.com",
        ValidateAudience = true,
        ValidAudience = "example.com",
        ValidateLifetime = true,
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("yourSecretKey")),
        ValidateIssuerSigningKey = true,
    };
});
  1. 在Startup.cs中配置授权策略
services.AddAuthorization(options =>
{
    options.DefaultPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
});
  1. 在需要进行JWT身份验证的方法中获取用户信息
var identity = HttpContext.User.Identity as ClaimsIdentity;
var username = identity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

这样,你就可以在同一应用程序中同时使用Windows身份验证和JWT了。

相关内容

热门资讯

透视烘培!aapoker脚本怎... 透视烘培!aapoker脚本怎么用,AApoker ai一直存在有下载,第6分钟教程(有人有挂)1、...
透视总结!aapoker辅助怎... 透视总结!aapoker辅助怎么用,AApoker透视其实是有神器,十分钟教程(有挂分享)1、进入游...
透视绝活儿!hhpoker免费... 透视绝活儿!hhpoker免费透视脚本,HHpoker辅助器都是真的有插件,第四分钟教程(有挂技巧)...
透视步骤!wepoker手机助... 透视步骤!wepoker手机助手,WePoKer购买一贯真的是有透视,一分钟教程(发现有挂)1、任何...
透视机巧!sohoo poke... 透视机巧!sohoo poker辅助,WePoKer购买确实是有脚本,第三分钟教程(有挂教程)1、s...
透视手段!hhpoker万能辅... 透视手段!hhpoker万能辅助器,HHpoker透视器都是有插件,两分钟教程(新版有挂)1、超多福...
透视要领!wepoker一直输... 透视要领!wepoker一直输的号能继续打吗,WePoKer数据切实是真的有神器,2分钟教程(证实有...
透视指南书!fishpoker... 透视指南书!fishpoker透视,WePoKer新版切实真的是有工具,第3分钟教程(新版有挂)1、...
透视指南!aapoker辅助软... 透视指南!aapoker辅助软件合法吗,AApoker软件本来真的有透视,7分钟教程(详细教程)1、...
透视经验!hhpoker有没有... 透视经验!hhpoker有没有作必弊辅助,HHpoker工具一直存在有工具,第5分钟教程(今日头条)...