asp.net-core-signalr
基于asp net core signalr 实现简单的demo,并采用授权机制。
1、使用JWT进行授权认证
- 添加授权自定义策略
services.AddAuthorization(options =>
{
options.AddPolicy("Hubs", policy => policy.Requirements.Add(new PolicyRequirement()));
})
- 设置认证方式(cookie、bearer、openid)
AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
- 添加JWT认证机制
-
设置验证参数
x.TokenValidationParameters = new TokenValidationParameters { LifetimeValidator = (before, expires, token, param) => { return expires > DateTime.UtcNow; }, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key)),//Secret ValidateIssuer = false, ValidateAudience = false, ValidateActor = false, ValidateLifetime = true };
-
为Jwt注册事件
x.Events = new JwtBearerEvents { OnMessageReceived = context => { var accessToken =</
-