Skip to content

Commit 6fbf252

Browse files
Merge pull request #262527 from vicancy/patch-11
Update signalr-concept-client-negotiation.md
2 parents c11bd2a + 6ff9843 commit 6fbf252

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

articles/azure-signalr/signalr-concept-client-negotiation.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ Then your clients can request the function endpoint `https://<Your Function App
218218

219219
### Self-exposing `/negotiate` endpoint
220220

221-
You could also expose the negotiation endpoint in your own server and return the negotiation response by yourself if you are using other languages. Below is a pseudo code in JavaScript showing how to implement the negotiation endpoint for hub `chat`.
221+
You could also expose the negotiation endpoint in your own server and return the negotiation response by yourself if you are using other languages.
222+
223+
#### Using ConnectionString
224+
225+
Below is a pseudo code in JavaScript showing how to implement the negotiation endpoint for hub `chat` and generate access token from Azure SignalR connection string.
222226

223227
```js
224228
import express from 'express';
@@ -242,6 +246,44 @@ let connection = new signalR.HubConnectionBuilder().withUrl('/chat').build();
242246
connection.start();
243247
```
244248

249+
#### Using Microsoft Entra ID
250+
Azure SignalR also provides REST API `POST /api/hubs/${hub}/:generateToken?api-version=2022-11-01&userId=${userId}&minutesToExpire=${minutesToExpire}` to generate the client access token for you when you are using Microsoft Entra ID.
251+
252+
The steps are:
253+
1. Follow [Add role assignments](signalr-howto-authorize-application.md#add-role-assignments-in-the-azure-portal) to assign role `SignalR REST API Owner` or `SignalR Service Owner` to your identity so that your identity has the permission to invoke the REST API to generate the client access token.
254+
2. Use Azure Identity client library to fetch the Microsoft Entra ID token with scope `https://signalr.azure.com/.default`
255+
3. Use this token to visit the generate token REST API
256+
4. Return the client access token in the negotiation response.
257+
258+
Below is a pseudo code in JavaScript showing how to implement the negotiation endpoint for hub `chat` and get access token using Microsoft Entra ID and REST API `/generateToken`.
259+
```js
260+
import express from "express";
261+
import axios from "axios";
262+
import { DefaultAzureCredential } from "@azure/identity";
263+
264+
const endpoint = "https://<your-service>.service.signalr.net";
265+
const hub = "chat";
266+
const generateTokenUrl = `${endpoint}/api/hubs/${hub}/:generateToken?api-version=2022-11-01`;
267+
let app = express();
268+
app.get("/chat/negotiate", async (req, res) => {
269+
// use DefaultAzureCredential to get the Entra ID token to call the Azure SignalR REST API
270+
const credential = new DefaultAzureCredential();
271+
const entraIdToken = await credential.getToken("https://signalr.azure.com/.default");
272+
const token = (
273+
await axios.post(generateTokenUrl, undefined, {
274+
headers: {
275+
"content-type": "application/json",
276+
Authorization: `Bearer ${entraIdToken.token}`,
277+
},
278+
})
279+
).data.token;
280+
let url = `${endpoint}/client/?hub=${hub}`;
281+
res.json({ url: url, accessToken: token });
282+
});
283+
app.listen(8080, () => console.log("server started"));
284+
285+
```
286+
245287
## Next steps
246288

247289
To learn more about how to use default and serverless modes, see the following articles:

0 commit comments

Comments
 (0)