Pass HttpContext during negotiation (#183)
This commit is contained in:
Родитель
d6bdff0bcd
Коммит
e7cb9e4b08
|
@ -34,15 +34,12 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
|
|||
{
|
||||
var azureSignalRClient = Utils.GetAzureSignalRClient(attrResolved.ConnectionStringSetting, attrResolved.HubName);
|
||||
|
||||
if (!bindingData.ContainsKey(HttpRequestName) || securityTokenValidator == null)
|
||||
bindingData.TryGetValue(HttpRequestName, out var requestObj);
|
||||
var request = requestObj as HttpRequest;
|
||||
var httpContext = request?.HttpContext;
|
||||
|
||||
if (bindingData.ContainsKey(HttpRequestName) && securityTokenValidator != null)
|
||||
{
|
||||
var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken,
|
||||
attrResolved.ClaimTypeList);
|
||||
return new SignalRConnectionInfoValueProvider(info, userType, "");
|
||||
}
|
||||
|
||||
var request = bindingData[HttpRequestName] as HttpRequest;
|
||||
|
||||
var tokenResult = securityTokenValidator.ValidateToken(request);
|
||||
|
||||
if (tokenResult.Status != SecurityTokenStatus.Valid)
|
||||
|
@ -50,22 +47,20 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
|
|||
return new SignalRConnectionInfoValueProvider(null, userType, "");
|
||||
}
|
||||
|
||||
if (signalRConnectionInfoConfigurer == null)
|
||||
{
|
||||
var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken,
|
||||
attrResolved.ClaimTypeList);
|
||||
return new SignalRConnectionInfoValueProvider(info, userType, "");
|
||||
}
|
||||
|
||||
var signalRConnectionDetail = new SignalRConnectionDetail
|
||||
{
|
||||
UserId = attrResolved.UserId,
|
||||
Claims = azureSignalRClient.GetCustomClaims(attrResolved.IdToken, attrResolved.ClaimTypeList),
|
||||
};
|
||||
signalRConnectionInfoConfigurer.Configure(tokenResult, request, signalRConnectionDetail);
|
||||
signalRConnectionInfoConfigurer?.Configure(tokenResult, request, signalRConnectionDetail);
|
||||
var customizedInfo = await azureSignalRClient.GetClientConnectionInfoAsync(signalRConnectionDetail.UserId,
|
||||
signalRConnectionDetail.Claims);
|
||||
signalRConnectionDetail.Claims, httpContext);
|
||||
return new SignalRConnectionInfoValueProvider(customizedInfo, userType, "");
|
||||
}
|
||||
|
||||
var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken,
|
||||
attrResolved.ClaimTypeList, httpContext);
|
||||
return new SignalRConnectionInfoValueProvider(info, userType, "");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ using System.IdentityModel.Tokens.Jwt;
|
|||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Azure.SignalR;
|
||||
using Microsoft.Azure.SignalR.Management;
|
||||
|
||||
|
@ -40,16 +41,21 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
|
|||
this.connectionStringKey = connectionStringKey;
|
||||
}
|
||||
|
||||
public Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, string idToken, string[] claimTypeList)
|
||||
public Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, string idToken, string[] claimTypeList, HttpContext httpContext)
|
||||
{
|
||||
var customerClaims = GetCustomClaims(idToken, claimTypeList);
|
||||
return GetClientConnectionInfoAsync(userId, customerClaims);
|
||||
return GetClientConnectionInfoAsync(userId, customerClaims, httpContext);
|
||||
}
|
||||
|
||||
public async Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, IList<Claim> claims)
|
||||
public async Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, IList<Claim> claims, HttpContext httpContext)
|
||||
{
|
||||
var serviceHubContext = await GetHubContextAsync();
|
||||
var negotiateResponse = await serviceHubContext.NegotiateAsync(new NegotiationOptions() { UserId = userId, Claims = BuildJwtClaims(claims, AzureSignalRUserPrefix).ToList() });
|
||||
var negotiateResponse = await serviceHubContext.NegotiateAsync(new NegotiationOptions()
|
||||
{
|
||||
UserId = userId,
|
||||
Claims = BuildJwtClaims(claims, AzureSignalRUserPrefix).ToList(),
|
||||
HttpContext = httpContext
|
||||
});
|
||||
return new SignalRConnectionInfo
|
||||
{
|
||||
Url = negotiateResponse.Url,
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace SignalRServiceExtension.Tests
|
|||
var configuration = new ConfigurationBuilder().AddInMemoryCollection(configDict).Build();
|
||||
var serviceManagerStore = new ServiceManagerStore(configuration, NullLoggerFactory.Instance, new TestRouter());
|
||||
var azureSignalRClient = new AzureSignalRClient(serviceManagerStore, connectionStringKey, hubName);
|
||||
var connectionInfo = await azureSignalRClient.GetClientConnectionInfoAsync(userId, idToken, claimTypeList);
|
||||
var connectionInfo = await azureSignalRClient.GetClientConnectionInfoAsync(userId, idToken, claimTypeList, null);
|
||||
|
||||
Assert.Equal(connectionInfo.Url, $"{hubUrl}/client/?hub={hubName.ToLower()}");
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче