JwtAuthentication已禁用身份登录

时间:2019-05-09 07:20:36

标签: asp.net-mvc asp.net-core bootstrap-4 jwt asp.net-identity

因此,我在我的ASP.NET Core项目中添加了Jwt身份验证。该项目有两种交互方式:

1)直接使用Web服务。使用MVC控制器访问和移动事物。

2)单独的客户端。

我为客户端添加了JWT,以允许生成令牌并根据客户端请求进行请求。

对于客户端,它正在运行。但是,它在Web服务端已损坏。我正在使用Bootstrap4,现在无法使用网页登录。

这是Startup.cs的一部分:

private class ConfigureJwtBearerOptions : IPostConfigureOptions<JwtBearerOptions> {
  private readonly IOptions<JwtAuthentication> _jwtAuthentication;

  public ConfigureJwtBearerOptions(IOptions<JwtAuthentication> jwtAuthentication) {
    _jwtAuthentication = jwtAuthentication ?? throw new System.ArgumentNullException(nameof(jwtAuthentication));
  }

  public void PostConfigure(string name, JwtBearerOptions options) {
    var jwtAuthentication = _jwtAuthentication.Value;

    options.ClaimsIssuer = jwtAuthentication.ValidIssuer;
    options.IncludeErrorDetails = true;
    options.RequireHttpsMetadata = true;
    options.TokenValidationParameters = new TokenValidationParameters {
      ValidateActor = true,
      ValidateIssuer = true,
      ValidateAudience = true,
      ValidateLifetime = true,
      ValidateIssuerSigningKey = true,
      ValidIssuer = jwtAuthentication.ValidIssuer,
      ValidAudience = jwtAuthentication.ValidAudience,
      IssuerSigningKey = jwtAuthentication.SymmetricSecurityKey,
      NameClaimType = ClaimTypes.NameIdentifier
    };
  }
}

然后我使用该私有类添加身份验证:

  services.AddSingleton<IPostConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>();
  services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
      .AddJwtBearer();

最后,

  app.UseAuthentication();

现在,如果我注释掉services.AddAuthentication(...).AddJwtBearer(),我的Web服务将再次运行,并且Bootstrap4将允许我登录并执行我想做的任何事情。但是,这样做的问题是它无法正确认证客户端程序。

是否可以使用Jwt身份验证方法对Bootstrap4登录进行身份验证?

注意:_LoginPartial.cshtml默认为:

<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
    <li class="nav-item">
        <a  class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
    </li>
    <li class="nav-item">
        <form  class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
            <button  type="submit" class="nav-link btn btn-link text-dark">Logout</button>
        </form>
    </li>
}
else
{
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
    </li>
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
    </li>
}
</ul>

启用身份验证后,条件@if (SignInManager.IsSignedIn(User))永远不会返回true。

1 个答案:

答案 0 :(得分:0)

显然,最好不要使用Jwt身份验证来验证身份,这是因为很难在网站上传递令牌,这很困难。

我发现了这个article,它描述了如何具有两种身份验证方案。

基本上,我替换了这段代码:

services.AddSingleton<IPostConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  .AddJwtBearer();

与此:

  services.AddSingleton<IPostConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>();
  services.AddAuthentication()
    .AddCookie(cfg => cfg.SlidingExpiration = true)
    .AddJwtBearer(cfg => {
      cfg.RequireHttpsMetadata = false;
      cfg.SaveToken = true;

      cfg.TokenValidationParameters = new TokenValidationParameters() {
        ValidIssuer = Configuration["JwtAuthentication:ValidIssuer"],
        ValidAudience = Configuration["JwtAuthentication:ValidAudience"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtAuthentication:SecurityKey"]))
      };
    });

现在两种身份验证方案都可以使用。

这是我的最佳解决方案,因为我有两个完全独立的控制器来完成工作:

1)MVC控制器:使用身份验证

2)API控制器:使用JWT身份验证

相关问题