【问题标题】:. Net core 2.0 windows and jwt authentication. Net core 2.0 windows 和 jwt 身份验证
【发布时间】:2018-06-22 22:14:57
【问题描述】:

是否可以在同一个项目中实现windows和jwt认证方案?

我需要 Windows 身份验证来捕获没有任何登录页面的用户和 jwt 来处理任何其他页面和 wep api 的角色。

【问题讨论】:

    标签: windows authentication jwt asp.net-core-2.0


    【解决方案1】:

    是的,您可以向您的应用程序添加多个身份验证方案。参考以下link

    我终于让两者都工作了。我在互联网上没有找到任何已解决的示例,希望这可以帮助任何寻找答案的人。

                services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = IISDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = "Bearer";
                }).AddJwtBearer("Bearer", options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateAudience = false,
                        //ValidAudience = "the audience you want to validate",
                        ValidateIssuer = false,
                        //ValidIssuer = "the isser you want to validate",
    
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("myapisecretkey")),
    
                        ValidateLifetime = true, //validate the expiration and not before values in the token
    
                        ClockSkew = TimeSpan.FromMinutes(5) //5 minute tolerance for the expiration date
                    };
                });
    
                services.AddAuthorization(auth =>
                {
                    auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                        .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​)
                        .RequireClaim(ClaimTypes.Name, "MyAPIUser").Build());
                });
    

    然后通过装饰选择您要在特定控制器上使用的身份验证方案。

    [Route("api/MyController")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class MyController : Controller
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 2018-03-24
      • 1970-01-01
      • 2018-03-01
      • 2018-12-05
      • 2018-01-28
      • 2018-03-13
      • 2019-11-28
      • 2017-09-11
      相关资源
      最近更新 更多