1 回答

TA貢獻1818條經驗 獲得超3個贊
您需要對 JWT Bearer 事件進行特殊處理,以便您的身份驗證正常工作。令牌需要轉發到集線器??纯次艺f的部分
那部分不見了
public void ConfigureServices(IServiceCollection services)
{
? ? services.AddDbContext<ApplicationDbContext>(options =>
? ? ? ? options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
? ? services.AddIdentity<ApplicationUser, IdentityRole>()
? ? ? ? .AddEntityFrameworkStores<ApplicationDbContext>()
? ? ? ? .AddDefaultTokenProviders();
? ? services.AddAuthentication(options =>
? ? ? ? {
? ? ? ? ? ? // Identity made Cookie authentication the default.
? ? ? ? ? ? // However, we want JWT Bearer Auth to be the default.
? ? ? ? ? ? options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
? ? ? ? ? ? options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
? ? ? ? })
? ? ? ? .AddJwtBearer(options =>
? ? ? ? {
? ? ? ? ? ? // Configure JWT Bearer Auth to expect our security key
? ? ? ? ? ? options.TokenValidationParameters =
? ? ? ? ? ? ? ? new TokenValidationParameters
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? LifetimeValidator = (before, expires, token, param) =>
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? return expires > DateTime.UtcNow;
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ValidateAudience = false,
? ? ? ? ? ? ? ? ? ? ValidateIssuer = false,
? ? ? ? ? ? ? ? ? ? ValidateActor = false,
? ? ? ? ? ? ? ? ? ? ValidateLifetime = true,
? ? ? ? ? ? ? ? ? ? IssuerSigningKey = SecurityKey
? ? ? ? ? ? ? ? };
? ? ? ? ? ? //THAT IS THE PART WHICH IS MISSING IN YOUR CONFIG !
? ? ? ? ? ? // We have to hook the OnMessageReceived event in order to
? ? ? ? ? ? // allow the JWT authentication handler to read the access
? ? ? ? ? ? // token from the query string when a WebSocket or?
? ? ? ? ? ? // Server-Sent Events request comes in.
? ? ? ? ? ? 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("/hubs/chat")))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? // Read the token out of the query string
? ? ? ? ? ? ? ? ? ? ? ? context.Token = accessToken;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return Task.CompletedTask;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? });
? ? services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
? ? services.AddSignalR();
? ? // Change to use Name as the user identifier for SignalR
? ? // WARNING: This requires that the source of your JWT token?
? ? // ensures that the Name claim is unique!
? ? // If the Name claim isn't unique, users could receive messages?
? ? // intended for a different user!
? ? services.AddSingleton<IUserIdProvider, NameUserIdProvider>();
? ? // Change to use email as the user identifier for SignalR
? ? // services.AddSingleton<IUserIdProvider, EmailBasedUserIdProvider>();
? ? // WARNING: use *either* the NameUserIdProvider *or* the?
? ? // EmailBasedUserIdProvider, but do not use both.?
}
- 1 回答
- 0 關注
- 200 瀏覽
添加回答
舉報