|
| 1 | +using Microsoft.AspNetCore.Authentication.JwtBearer; |
| 2 | +using Microsoft.IdentityModel.Tokens; |
| 3 | +using System.IdentityModel.Tokens.Jwt; |
| 4 | +using System.Security.Claims; |
| 5 | +using System.Security.Cryptography; |
| 6 | +using UbikLink.Commander.Test; |
| 7 | +using UbikLink.Common.Api; |
| 8 | +using UbikLink.Common.Auth; |
| 9 | + |
| 10 | +//var SecurityKey = new SymmetricSecurityKey(RandomNumberGenerator.GetBytes(32)); |
| 11 | + |
| 12 | +var builder = WebApplication.CreateBuilder(args); |
| 13 | + |
| 14 | +builder.AddServiceDefaults(); |
| 15 | + |
| 16 | +// Add services to the container. |
| 17 | +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi |
| 18 | +//builder.Services.AddOpenApi(); |
| 19 | +builder.Services.AddSignalR(); |
| 20 | +builder.Services.AddCors(); |
| 21 | + |
| 22 | +var keys = new AuthRegisterAuthKey(); |
| 23 | +builder.Configuration.GetSection(AuthRegisterAuthKey.Position).Bind(keys); |
| 24 | +var SecurityKey = new SymmetricSecurityKey(Convert.FromBase64String(keys.HubSignSecureKey)); |
| 25 | + |
| 26 | + |
| 27 | +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| 28 | + .AddJwtBearer(options => |
| 29 | + { |
| 30 | + options.TokenValidationParameters = |
| 31 | + new TokenValidationParameters |
| 32 | + { |
| 33 | + LifetimeValidator = (before, expires, token, parameters) => expires > DateTime.UtcNow, |
| 34 | + ValidateAudience = false, |
| 35 | + ValidateIssuer = false, |
| 36 | + ValidateActor = false, |
| 37 | + ValidateLifetime = true, |
| 38 | + IssuerSigningKey = SecurityKey |
| 39 | + }; |
| 40 | + |
| 41 | + options.Events = new JwtBearerEvents |
| 42 | + { |
| 43 | + OnMessageReceived = context => |
| 44 | + { |
| 45 | + var accessToken = context.Request.Query["access_token"]; |
| 46 | + |
| 47 | + if (!string.IsNullOrEmpty(accessToken)) |
| 48 | + { |
| 49 | + context.Token = context.Request.Query["access_token"]; |
| 50 | + } |
| 51 | + return Task.CompletedTask; |
| 52 | + } |
| 53 | + }; |
| 54 | + }); |
| 55 | + |
| 56 | +builder.Services.AddAuthorizationBuilder() |
| 57 | + .AddPolicy(JwtBearerDefaults.AuthenticationScheme, policy => |
| 58 | + { |
| 59 | + policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); |
| 60 | + policy.RequireClaim(ClaimTypes.NameIdentifier); |
| 61 | + policy.RequireClaim(ClaimTypes.UserData); |
| 62 | + }); |
| 63 | + |
| 64 | + |
| 65 | +var app = builder.Build(); |
| 66 | + |
| 67 | +app.MapDefaultEndpoints(); |
| 68 | + |
| 69 | +// Configure the HTTP request pipeline. |
| 70 | +if (app.Environment.IsDevelopment()) |
| 71 | +{ |
| 72 | + //app.MapOpenApi(); |
| 73 | +} |
| 74 | + |
| 75 | +app.UseAuthentication(); |
| 76 | +app.UseAuthorization(); |
| 77 | + |
| 78 | +app.UseCors(x => x |
| 79 | + .AllowAnyMethod() |
| 80 | + .AllowAnyHeader() |
| 81 | + .SetIsOriginAllowed(origin => true) |
| 82 | + .AllowCredentials()); |
| 83 | + |
| 84 | +app.MapHub<ChatHub>("/chat"); |
| 85 | + |
| 86 | +//app.UseHttpsRedirection(); |
| 87 | + |
| 88 | + |
| 89 | +app.Run(); |
0 commit comments