diff --git a/samples/AdvancedChatRoom/AdvancedChatRoom.csproj b/samples/AdvancedChatRoom/AdvancedChatRoom.csproj index 7c5ba762..c443c930 100644 --- a/samples/AdvancedChatRoom/AdvancedChatRoom.csproj +++ b/samples/AdvancedChatRoom/AdvancedChatRoom.csproj @@ -1,6 +1,6 @@ - net8.0 + net11.0 advancedchatroom Microsoft.Azure.SignalR.Samples.AdvancedChatRoom @@ -10,7 +10,7 @@ - - + + diff --git a/samples/AdvancedChatRoom/Controller/JwtController.cs b/samples/AdvancedChatRoom/Controller/JwtController.cs index 0ca2d18d..e91398f1 100644 --- a/samples/AdvancedChatRoom/Controller/JwtController.cs +++ b/samples/AdvancedChatRoom/Controller/JwtController.cs @@ -6,6 +6,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; +using System.Security.Cryptography; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; @@ -14,7 +15,7 @@ namespace Microsoft.Azure.SignalR.Samples.AdvancedChatRoom [Route("jwt")] public class JwtController : Controller { - private static readonly SecurityKey SigningKey = new SymmetricSecurityKey(Guid.NewGuid().ToByteArray()); + private static readonly SecurityKey SigningKey = new SymmetricSecurityKey(RandomNumberGenerator.GetBytes(32)); private static readonly JwtSecurityTokenHandler JwtTokenHandler = new JwtSecurityTokenHandler(); diff --git a/samples/AdvancedChatRoom/Dockerfile b/samples/AdvancedChatRoom/Dockerfile index 0f591390..8176e751 100644 --- a/samples/AdvancedChatRoom/Dockerfile +++ b/samples/AdvancedChatRoom/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build WORKDIR /src/AdvancedChatRoom COPY ./. ./ diff --git a/samples/AdvancedChatRoom/README.md b/samples/AdvancedChatRoom/README.md index d7b6036d..d78365f3 100644 --- a/samples/AdvancedChatRoom/README.md +++ b/samples/AdvancedChatRoom/README.md @@ -12,22 +12,26 @@ Now the sample supports: * Send to User / Users * Cookie / JWT based Authentication * Role / Claim / Policy based Authrization +* JWT authentication refresh without reconnecting ## Run the sample locally +Authentication refresh requires SignalR JavaScript client 11.0.0 or later and Azure SignalR SDK 1.34.0 or later. + Type the following commands to run this app. -``` +```powershell dotnet restore -dotnet user-secrets set Azure:SignalR:ConnectionString "" +$env:Azure__SignalR__ConnectionString = "" dotnet run ``` -## Quick Deploy via Docker image -You can also deploy this sample via existing docker image +## Run via Docker +Build the image from this sample directory: ``` +docker build -t advancedchatroom . docker run -e Azure__SignalR__ConnectionString="" \ - -p 5000:80 mcr.microsoft.com/signalrsamples/advancedchatroom:latest + -p 5000:80 advancedchatroom ``` Open the broswer with url `localhost:5000`, you can see the sample just like Chat Sample but has more operations. diff --git a/samples/AdvancedChatRoom/Startup.cs b/samples/AdvancedChatRoom/Startup.cs index 80bcb175..842fb4aa 100644 --- a/samples/AdvancedChatRoom/Startup.cs +++ b/samples/AdvancedChatRoom/Startup.cs @@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.IdentityModel.Tokens; +using Microsoft.IdentityModel.JsonWebTokens; namespace Microsoft.Azure.SignalR.Samples.AdvancedChatRoom { @@ -53,6 +54,18 @@ public void ConfigureServices(IServiceCollection services) ValidAudience = JwtController.Audience, IssuerSigningKey = JwtController.SigningCreds.Key }; + option.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + if (context.SecurityToken is JsonWebToken jwt) + { + context.Properties.ExpiresUtc = new DateTimeOffset(jwt.ValidTo, TimeSpan.Zero); + } + + return Task.CompletedTask; + } + }; }); services.AddControllers(); @@ -75,7 +88,11 @@ public void Configure(IApplicationBuilder app) app.UseEndpoints(endpoints => { endpoints.MapControllers(); - endpoints.MapHub("/chatjwt"); + endpoints.MapHub("/chatjwt", options => + { + options.EnableAuthenticationRefresh = true; + options.CloseOnAuthenticationExpiration = true; + }); endpoints.MapHub("/chatcookie"); }); } diff --git a/samples/AdvancedChatRoom/wwwroot/index.html b/samples/AdvancedChatRoom/wwwroot/index.html index 3e7cfce7..beda513d 100644 --- a/samples/AdvancedChatRoom/wwwroot/index.html +++ b/samples/AdvancedChatRoom/wwwroot/index.html @@ -13,6 +13,10 @@

Azure SignalR Group Chat

+
@@ -20,6 +24,7 @@

+

@@ -58,7 +63,7 @@

+