Skip to content

Commit 68a9176

Browse files
committed
Add samples for MicrosoftEntra
1 parent 3bebe71 commit 68a9176

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2258
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.*" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
using Microsoft.AspNetCore.SignalR;
4+
5+
public class ChatSampleHub : Hub
6+
{
7+
public Task BroadcastMessage(string name, string message) =>
8+
Clients.All.SendAsync("broadcastMessage", name, message);
9+
10+
public Task Echo(string name, string message) =>
11+
Clients.Client(Context.ConnectionId)
12+
.SendAsync("echo", name, $"{message} (echo from server)");
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
using Azure.Identity;
4+
5+
using Microsoft.Azure.SignalR;
6+
7+
const string Endpoint = "https://<resourceName>.service.signalr.net";
8+
9+
var builder = WebApplication.CreateBuilder(args);
10+
builder.Services.AddSignalR().AddAzureSignalR(option =>
11+
{
12+
var appTenantId = "<guid>";
13+
var appClientId = "<guid>";
14+
var clientCertificatePath = "<your path to certificate>";
15+
var credential = new ClientCertificateCredential(appTenantId, appClientId, clientCertificatePath);
16+
var endpoint = new ServiceEndpoint(new Uri(Endpoint), credential);
17+
option.Endpoints = [endpoint];
18+
});
19+
var app = builder.Build();
20+
21+
app.UseHttpsRedirection();
22+
app.UseDefaultFiles();
23+
app.UseStaticFiles();
24+
app.UseRouting();
25+
26+
app.MapHub<ChatSampleHub>("/chat");
27+
28+
app.Run();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"ApplicationWithCertificates": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"environmentVariables": {
7+
"ASPNETCORE_ENVIRONMENT": "Development"
8+
},
9+
"applicationUrl": "https://localhost:55450;http://localhost:55451"
10+
}
11+
}
12+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*html, body {
2+
font-size: 16px;
3+
}
4+
5+
@media all and (max-device-width: 720px) {
6+
html, body {
7+
font-size: 20px;
8+
}
9+
}*/
10+
11+
html, body {
12+
padding: 0;
13+
height: 100%;
14+
}
15+
16+
#messages {
17+
width: 100%;
18+
border: 1px solid #ccc;
19+
height: calc(100% - 120px);
20+
float: none;
21+
margin: 0px auto;
22+
padding-left: 0px;
23+
overflow-y: auto;
24+
}
25+
26+
textarea:focus {
27+
outline: none !important;
28+
}
29+
30+
.system-message {
31+
background: #87CEFA;
32+
}
33+
34+
.broadcast-message {
35+
display: inline-block;
36+
background: yellow;
37+
margin: auto;
38+
padding: 5px 10px;
39+
}
40+
41+
.message-entry {
42+
overflow: auto;
43+
margin: 8px 0;
44+
}
45+
46+
.message-avatar {
47+
display: inline-block;
48+
padding: 10px;
49+
max-width: 8em;
50+
word-wrap: break-word;
51+
}
52+
53+
.message-content {
54+
display: inline-block;
55+
background-color: #b2e281;
56+
padding: 10px;
57+
margin: 0 0.5em;
58+
max-width: calc(60%);
59+
word-wrap: break-word;
60+
}
61+
62+
.message-content.pull-left:before {
63+
width: 0;
64+
height: 0;
65+
display: inline-block;
66+
float: left;
67+
border-top: 10px solid transparent;
68+
border-bottom: 10px solid transparent;
69+
border-right: 10px solid #b2e281;
70+
margin: 15px 0;
71+
}
72+
73+
.message-content.pull-right:after {
74+
width: 0;
75+
height: 0;
76+
display: inline-block;
77+
float: right;
78+
border-top: 10px solid transparent;
79+
border-bottom: 10px solid transparent;
80+
border-left: 10px solid #b2e281;
81+
margin: 15px 0;
82+
}
Binary file not shown.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
5+
<meta name="viewport" content="width=device-width">
6+
<meta http-equiv="Pragma" content="no-cache" />
7+
<meta http-equiv="Expires" content="0" />
8+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
9+
<link href="css/site.css" rel="stylesheet" />
10+
<title>Azure SignalR Group Chat</title>
11+
</head>
12+
13+
<body>
14+
<h2 class="text-center" style="margin-top: 0; padding-top: 30px; padding-bottom: 30px;">Azure SignalR Group Chat</h2>
15+
<div class="container" style="height: calc(100% - 110px);">
16+
<div id="messages" style="background-color: whitesmoke; "></div>
17+
<div style="width: 100%; border-left-style: ridge; border-right-style: ridge;">
18+
<textarea id="message" style="width: 100%; padding: 5px 10px; border-style: hidden;"
19+
placeholder="Type message and press Enter to send..."></textarea>
20+
</div>
21+
<div style="overflow: auto; border-style: ridge; border-top-style: hidden;">
22+
<button class="btn-warning pull-right" id="echo">Echo</button>
23+
<button class="btn-success pull-right" id="sendmessage">Send</button>
24+
</div>
25+
</div>
26+
<div class="modal alert alert-danger fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
27+
<div class="modal-dialog" role="document">
28+
<div class="modal-content">
29+
<div class="modal-header">
30+
<div>Connection Error...</div>
31+
<div><strong style="font-size: 1.5em;">Hit Refresh/F5</strong> to rejoin. ;)</div>
32+
</div>
33+
</div>
34+
</div>
35+
</div>
36+
37+
<!--Reference the SignalR library. -->
38+
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script>
39+
40+
<!--Add script to update the page and send messages.-->
41+
<script type="text/javascript">
42+
document.addEventListener("DOMContentLoaded", function () {
43+
function getUserName() {
44+
function generateRandomName() {
45+
return Math.random().toString(36).substring(2, 10);
46+
}
47+
48+
// Get the user name and store it to prepend to messages.
49+
var username = generateRandomName();
50+
var promptMessage = "Enter your name:";
51+
do {
52+
username = prompt(promptMessage, username);
53+
if (!username || username.startsWith("_") || username.indexOf("<") > -1 || username.indexOf(">") > -1) {
54+
username = "";
55+
promptMessage = "Invalid input. Enter your name:";
56+
}
57+
} while (!username)
58+
return username;
59+
}
60+
61+
username = getUserName();
62+
// Set initial focus to message input box.
63+
var messageInput = document.getElementById("message");
64+
messageInput.focus();
65+
66+
function createMessageEntry(encodedName, encodedMsg) {
67+
var entry = document.createElement("div");
68+
entry.classList.add("message-entry");
69+
if (encodedName === "_SYSTEM_") {
70+
entry.innerHTML = encodedMsg;
71+
entry.classList.add("text-center");
72+
entry.classList.add("system-message");
73+
} else if (encodedName === "_BROADCAST_") {
74+
entry.classList.add("text-center");
75+
entry.innerHTML = `<div class="text-center broadcast-message">${encodedMsg}</div>`;
76+
} else if (encodedName === username) {
77+
entry.innerHTML = `<div class="message-avatar pull-right">${encodedName}</div>` +
78+
`<div class="message-content pull-right">${encodedMsg}<div>`;
79+
} else {
80+
entry.innerHTML = `<div class="message-avatar pull-left">${encodedName}</div>` +
81+
`<div class="message-content pull-left">${encodedMsg}<div>`;
82+
}
83+
return entry;
84+
}
85+
86+
function appendMessage(encodedName, encodedMsg) {
87+
var messageEntry = createMessageEntry(encodedName, encodedMsg);
88+
var messageBox = document.getElementById("messages");
89+
messageBox.appendChild(messageEntry);
90+
messageBox.scrollTop = messageBox.scrollHeight;
91+
}
92+
93+
function bindConnectionMessage(connection) {
94+
var messageCallback = function (name, message) {
95+
if (!message) return;
96+
// Html encode display name and message.
97+
var encodedName = name;
98+
var encodedMsg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
99+
appendMessage(encodedName, encodedMsg);
100+
};
101+
// Create a function that the hub can call to broadcast messages.
102+
connection.on("broadcastMessage", messageCallback);
103+
connection.on("echo", messageCallback);
104+
connection.onclose(onConnectionError);
105+
}
106+
107+
function onConnected(connection) {
108+
console.log("connection started");
109+
connection.send("broadcastMessage", "_SYSTEM_", username + " JOINED");
110+
document.getElementById("sendmessage").addEventListener("click", function (event) {
111+
// Call the broadcastMessage method on the hub.
112+
if (messageInput.value) {
113+
connection.send("broadcastMessage", username, messageInput.value)
114+
.catch((e) => appendMessage("_BROADCAST_", e.message));
115+
}
116+
117+
// Clear text box and reset focus for next comment.
118+
messageInput.value = "";
119+
messageInput.focus();
120+
event.preventDefault();
121+
});
122+
document.getElementById("message").addEventListener("keypress", function (event) {
123+
if (event.keyCode === 13) {
124+
event.preventDefault();
125+
document.getElementById("sendmessage").click();
126+
return false;
127+
}
128+
});
129+
document.getElementById("echo").addEventListener("click", function (event) {
130+
// Call the echo method on the hub.
131+
connection.send("echo", username, messageInput.value);
132+
133+
// Clear text box and reset focus for next comment.
134+
messageInput.value = "";
135+
messageInput.focus();
136+
event.preventDefault();
137+
});
138+
}
139+
140+
function onConnectionError(error) {
141+
if (error && error.message) {
142+
console.error(error.message);
143+
}
144+
var modal = document.getElementById("myModal");
145+
modal.classList.add("in");
146+
modal.style = "display: block;";
147+
}
148+
149+
var connection = new signalR.HubConnectionBuilder()
150+
.withUrl("/chat")
151+
.build();
152+
bindConnectionMessage(connection);
153+
connection.start()
154+
.then(function () {
155+
onConnected(connection);
156+
})
157+
.catch(function (error) {
158+
console.error(error.message);
159+
});
160+
});
161+
</script>
162+
</body>
163+
164+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.*" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
using Microsoft.AspNetCore.SignalR;
4+
5+
public class ChatSampleHub : Hub
6+
{
7+
public Task BroadcastMessage(string name, string message) =>
8+
Clients.All.SendAsync("broadcastMessage", name, message);
9+
10+
public Task Echo(string name, string message) =>
11+
Clients.Client(Context.ConnectionId)
12+
.SendAsync("echo", name, $"{message} (echo from server)");
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
using Azure.Identity;
4+
5+
using Microsoft.Azure.SignalR;
6+
7+
const string Endpoint = "https://<resourceName>.service.signalr.net";
8+
9+
var builder = WebApplication.CreateBuilder(args);
10+
builder.Services.AddSignalR().AddAzureSignalR(option =>
11+
{
12+
var appTenantId = "<guid>";
13+
var appClientId = "<guid>";
14+
var clientSecret = "<client secret>";
15+
var credential = new ClientSecretCredential(appTenantId, appClientId, clientSecret);
16+
var endpoint = new ServiceEndpoint(new Uri(Endpoint), credential);
17+
option.Endpoints = [endpoint];
18+
});
19+
var app = builder.Build();
20+
21+
app.UseHttpsRedirection();
22+
app.UseDefaultFiles();
23+
app.UseStaticFiles();
24+
app.UseRouting();
25+
26+
app.MapHub<ChatSampleHub>("/chat");
27+
28+
app.Run();

0 commit comments

Comments
 (0)