-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathNegotiateController.cs
70 lines (61 loc) · 2.46 KB
/
NegotiateController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.SignalR.Management;
using Microsoft.Extensions.Configuration;
namespace NegotiationServer.Controllers
{
[ApiController]
public class NegotiateController : ControllerBase
{
private const string EnableDetailedErrors = "EnableDetailedErrors";
private readonly ServiceHubContext _hubContext;
private readonly ServiceHubContext<IMessageClient> _stronglyTypedHubContext;
private readonly bool _enableDetailedErrors;
public NegotiateController(IHubContextStore store, IConfiguration configuration)
{
_hubContext = store.HubContext;
_stronglyTypedHubContext = store.StronglyTypedHubContext;
_enableDetailedErrors = configuration.GetValue(EnableDetailedErrors, false);
}
[HttpPost("hub/negotiate")]
public async Task<ActionResult> HubNegotiate(string user)
{
if (string.IsNullOrEmpty(user))
{
return BadRequest("User ID is null or empty.");
}
var negotiateResponse = await _hubContext.NegotiateAsync(new()
{
UserId = user,
EnableDetailedErrors = _enableDetailedErrors
});
return new JsonResult(new Dictionary<string, string>()
{
{ "url", negotiateResponse.Url },
{ "accessToken", negotiateResponse.AccessToken }
});
}
//The negotiation of strongly typed hub has little difference with untyped hub.
[HttpPost("stronglyTypedHub/negotiate")]
public async Task<ActionResult> StronglyTypedHubNegotiate(string user)
{
if (string.IsNullOrEmpty(user))
{
return BadRequest("User ID is null or empty.");
}
var negotiateResponse = await _stronglyTypedHubContext.NegotiateAsync(new()
{
UserId = user,
EnableDetailedErrors = _enableDetailedErrors
});
return new JsonResult(new Dictionary<string, string>()
{
{ "url", negotiateResponse.Url },
{ "accessToken", negotiateResponse.AccessToken }
});
}
}
}