62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Reflection;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Connections;
|
|
|
|
namespace Microsoft.AspNetCore.SignalR
|
|
{
|
|
/// <summary>
|
|
/// Maps incoming requests to <see cref="Hub"/> types.
|
|
/// </summary>
|
|
public class HubRouteBuilder
|
|
{
|
|
private readonly ConnectionsRouteBuilder _routes;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="HubRouteBuilder"/> class.
|
|
/// </summary>
|
|
/// <param name="routes">The routes builder.</param>
|
|
public HubRouteBuilder(ConnectionsRouteBuilder routes)
|
|
{
|
|
_routes = routes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Maps incoming requests with the specified path to the specified <see cref="Hub"/> type.
|
|
/// </summary>
|
|
/// <typeparam name="THub">The <see cref="Hub"/> type to map requests to.</typeparam>
|
|
/// <param name="path">The request path.</param>
|
|
public void MapHub<THub>(PathString path) where THub : Hub
|
|
{
|
|
MapHub<THub>(path, configureOptions: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Maps incoming requests with the specified path to the specified <see cref="Hub"/> type.
|
|
/// </summary>
|
|
/// <typeparam name="THub">The <see cref="Hub"/> type to map requests to.</typeparam>
|
|
/// <param name="path">The request path.</param>
|
|
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
|
|
public void MapHub<THub>(PathString path, Action<HttpConnectionDispatcherOptions> configureOptions) where THub : Hub
|
|
{
|
|
// find auth attributes
|
|
var authorizeAttributes = typeof(THub).GetCustomAttributes<AuthorizeAttribute>(inherit: true);
|
|
var options = new HttpConnectionDispatcherOptions();
|
|
foreach (var attribute in authorizeAttributes)
|
|
{
|
|
options.AuthorizationData.Add(attribute);
|
|
}
|
|
configureOptions?.Invoke(options);
|
|
|
|
_routes.MapConnections(path, options, builder =>
|
|
{
|
|
builder.UseHub<THub>();
|
|
});
|
|
}
|
|
}
|
|
}
|