Auth0,.NETCoreApp 3.1和SignalR:获取解码的JWT

问题描述

我开发了一个聊天应用程序,该应用程序使用socket.io进行客户端和服务器之间的通信,但是在尝试跨多个节点扩展时遇到了严重的问题。所以我决定试一试SignalR ...

我正在使用Auth0进行身份验证,并且以前已经开发了一个库,以允许Auth0与socket.io集成。现在,我试图了解如何将Auth0与SignalR和我的React前端集成。

进展顺利,我已经设法通过套接字/ HTTP握手对应用程序进行身份验证,但是我努力了解如何从解码的JWT中获取子项和其他信息,因为该子项可以唯一地标识我的用户通常是该应用程序,尤其是SignalR集线器。

困难之处在于身份验证是通过HTTP进行的,因此该集线器当时不可用(或者是?告诉您我不知道我在说什么...)

我对ASP.NET Core和SignalR完全陌生,所以我认为自己做得很好!但是有人可以帮助我在中心内获得JWT吗?这是我的startup.cs

public class Startup
{
  public Startup(IConfiguration configuration)
  {
    Configuration = configuration;
  }

  public IConfiguration Configuration { get; }

  // This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddCors(options =>
    {
      options.AddPolicy("AllowSpecificOrigin",builder =>
        {
          builder
            .WithOrigins("http://localhost:3010","http://localhost:3000")
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials();
        });
    });

    var domain = $"https://{Configuration["Auth0:Domain"]}/";
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
      .AddJwtBearer(options =>
      {
        options.Authority = domain;
        options.Audience = Configuration["Auth0:Audience"];

        options.Events = new JwtBearerEvents
        {
          OnMessageReceived = context =>
          {
            var accesstoken = context.Request.Query["access_token"];

            // If the request is for our hub...
            var path = context.HttpContext.Request.Path;
            if (!string.IsNullOrEmpty(accesstoken) &&
                (path.StartsWithSegments("/chathub")))
            {
              // Read the token out of the query string
              context.Token = accesstoken;
            }
            return Task.CompletedTask;
          }
        };
      });

    services.AddAuthorization(options =>
    {
      options.AddPolicy("read:messages",policy => policy.Requirements.Add(new HasScopeRequirement("read:messages",domain)));
    });

    services.AddControllers();

    services.AddSignalR();

    // Register the scope authorization handler
    services.AddSingleton<IAuthorizationHandler,HasScopeHandler>();

  }

  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
  {
    if (env.IsDevelopment())
    {
      app.UseDeveloperExceptionPage();
    }
    else
    {
      app.UseHsts();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseCors("AllowSpecificOrigin");

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
      endpoints.MapControllers();
      endpoints.MapHub<ChatHub>("/chathub");
    });
  }
}

这是我的中心,例如:

namespace WebAPIApplication.Hubs
{
  [Authorize]
  public class ChatHub : Hub
  {
    public async Task SendMessage(string user,string message)
    {
      // How do I access the JWT sub here?
      await Clients.All.SendAsync("ReceiveMessage","Some message");
    }
  }
}

请帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)