Skip to content

Commit 2bd674e

Browse files
authored
Merge pull request #180184 from vicancy/aad
how to use aad in different languages
2 parents 84f1721 + 65495dd commit 2bd674e

8 files changed

+372
-81
lines changed

articles/azure-web-pubsub/concept-azure-ad-authorization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ When a security principal attempts to access a Web PubSub resource, the request
2626

2727
### Client-side authentication while using Azure AD
2828

29-
When using Access Key, the key is shared between your negotiation server (or Function App) and the Web PubSub resource, which means the Web PubSub service could authenticate the client connection request with the shared key. However, there is no shared key when using Azure AD to authorize.
29+
When using Access Key, the key is shared between your negotiation server (or Function App) and the Web PubSub resource, which means the Web PubSub service could authenticate the client connection request with the shared key. However, there is no access key when using Azure AD to authorize.
3030

3131
To solve this problem, we provided a REST API for generating the client token that can be used to connect to the Azure Web PubSub service.
3232

articles/azure-web-pubsub/howto-authorize-from-application.md

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -107,55 +107,14 @@ To learn more about how to assign and manage Azure role assignments, see these a
107107
- [Assign Azure roles using Azure CLI](../role-based-access-control/role-assignments-cli.md)
108108
- [Assign Azure roles using Azure Resource Manager templates](../role-based-access-control/role-assignments-template.md)
109109

110-
## Configure your server
110+
## Sample codes
111111

112-
It is recommended to configure identity and credentials in your environment variables:
112+
We officially support 4 programming languages:
113113

114-
| Variable | Description |
115-
|------|------
116-
| `AZURE_TENANT_ID` | The Azure Active Directory tenant(directory) ID. |
117-
| `AZURE_CLIENT_ID` | The client(application) ID of an App Registration in the tenant. |
118-
| `AZURE_CLIENT_SECRET` | A client secret that was generated for the App Registration. |
119-
| `AZURE_CLIENT_CERTIFICATE_PATH` | A path to certificate and private key pair in PEM or PFX format, which can authenticate the App Registration. |
120-
| `AZURE_USERNAME` | The username, also known as upn, of an Azure Active Directory user account. |
121-
| `AZURE_PASSWORD` | The password of the Azure Active Directory user account. Note this does not support accounts with MFA enabled. |
122-
123-
By doing this, you could use either [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) or [EnvironmentCredential](/dotnet/api/azure.identity.environmentcredential) to configure your Web PubSub endpoints.
124-
125-
### Sample codes
126-
127-
These are sample codes for C#. For other supported languages, see JavaScript/Python/Java.
128-
129-
```C#
130-
var endpoint = new Uri("https://<resource1>.webpubsub.azure.com");
131-
var client = new WebPubSubServiceClient(endpoint, "hub", new DefaultAzureCredential());
132-
```
133-
134-
To learn how `DefaultAzureCredential` works, see [DefaultAzureCredential Class](/dotnet/api/azure.identity.defaultazurecredential).
135-
136-
```C#
137-
var endpoint = new Uri("https://<resource1>.webpubsub.azure.com");
138-
var client = new WebPubSubServiceClient(endpoint, "hub", new EnvironmentCredential());
139-
```
140-
141-
You could also use [ClientSecretCredential](/dotnet/api/azure.identity.clientsecretcredential) or [ClientCertificateCredential](/dotnet/api/azure.identity.clientcertificatecredential) directly if you'd like to.
142-
143-
```C#
144-
var endpoint = new Uri("https://<resource1>.webpubsub.azure.com");
145-
var credential = new ClientSecretCredential("tenantId", "clientId", "clientSecret");
146-
var client = new WebPubSubServiceClient(endpoint, "hub", credential);
147-
```
148-
```C#
149-
var endpoint = new Uri("https://<resource1>.webpubsub.azure.com");
150-
var credential = new ClientCertificateCredential("tenantId", "clientId", "pathToCert");
151-
var client = new WebPubSubServiceClient(endpoint, "hub", credential);
152-
```
153-
154-
To learn more about creating `TokenCredential` for Azure AD Authorization, see there articles:
155-
156-
- [DefaultAzureCredential Class](/dotnet/api/azure.identity.defaultazurecredential)
157-
- [ClientSecretCredential Constructors](/dotnet/api/azure.identity.clientsecretcredential.-ctor)
158-
- [ClientCertificateCredential Constructors](/dotnet/api/azure.identity.clientcertificatecredential.-ctor)
114+
- [C#](./howto-create-serviceclient-with-net-and-azure-identity.md)
115+
- [Python](./howto-create-serviceclient-with-python-and-azure-identity.md)
116+
- [Java](./howto-create-serviceclient-with-java-and-azure-identity.md)
117+
- [JavaScript](./howto-create-serviceclient-with-javascript-and-azure-identity.md)
159118

160119
## Next steps
161120

articles/azure-web-pubsub/howto-authorize-from-managed-identity.md

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,41 +92,14 @@ To learn more about how to assign and manage Azure role assignments, see these a
9292
- [Assign Azure roles using Azure CLI](../role-based-access-control/role-assignments-cli.md)
9393
- [Assign Azure roles using Azure Resource Manager templates](../role-based-access-control/role-assignments-template.md)
9494

95-
## Sample codes while configuring your server
95+
## Sample codes
9696

97-
### Using system-assigned identity
97+
We officially support 4 programming languages:
9898

99-
You can use either [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) or [ManagedIdentityCredential](/dotnet/api/azure.identity.managedidentitycredential) to configure your Web PubSub endpoints while using system-assigned identity.
100-
101-
However, the best practice is to use `ManagedIdentityCredential` directly.
102-
103-
The system-assigned managed identity will be used by default, but **please make sure that you don't have configured any environment variables** that are preserved by [EnvironmentCredential](/dotnet/api/azure.identity.environmentcredential) if you were using `DefaultAzureCredential`. Otherwise it will fall back to use `EnvironmentCredential` to make the request and it will results to a `401 Unauthorized` response in most cases.
104-
105-
Here is sample codes for C#.
106-
107-
```C#
108-
var endpoint = new Uri("https://<resource1>.webpubsub.azure.com");
109-
var client = new WebPubSubServiceClient(endpoint, "hub", new ManagedIdentityCredential());
110-
```
111-
112-
There are also samples for other supported languages, see [Java](), [JavaScript](), [Python]().
113-
114-
### Using user-assigned identity
115-
116-
Simply provide `ClientId` while creating the `ManagedIdentityCredential` object.
117-
118-
> [!IMPORTANT]
119-
> Use **Client Id**, not the Object (principal) ID even if they looked similar!
120-
121-
Here is sample codes for C#.
122-
123-
```C#
124-
var endpoint = new Uri("https://<resource1>.webpubsub.azure.com");
125-
var clientId = "<your user-assigned identity client id>";
126-
var client = new WebPubSubServiceClient(endpoint, "hub", new ManagedIdentityCredential(clientId));
127-
```
128-
129-
There are also samples for other supported languages, see [Java](), [JavaScript](), [Python]().
99+
- [C#](./howto-create-serviceclient-with-net-and-azure-identity.md)
100+
- [Python](./howto-create-serviceclient-with-python-and-azure-identity.md)
101+
- [Java](./howto-create-serviceclient-with-java-and-azure-identity.md)
102+
- [JavaScript](./howto-create-serviceclient-with-javascript-and-azure-identity.md)
130103

131104
## Next steps
132105

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: How to create a WebPubSubServiceClient with Java and Azure Identity
3+
description: How to create a WebPubSubServiceClient with Java and Azure Identity
4+
author: terencefan
5+
6+
ms.author: tefa
7+
ms.date: 11/15/2021
8+
ms.service: azure-web-pubsub
9+
ms.topic: how-to
10+
---
11+
12+
# How to create a `WebPubSubServiceClient` with Java and Azure Identity
13+
14+
This how-to guide shows you how to create a `WebPubSubServiceClient` with Java and Azure Identity.
15+
16+
## Requirements
17+
18+
- Add [azure-identity](https://mvnrepository.com/artifact/com.azure/azure-identity) dependency in your `pom.xml`.
19+
20+
```xml
21+
<dependency>
22+
<groupId>com.azure</groupId>
23+
<artifactId>azure-identity</artifactId>
24+
<version>1.4.1</version>
25+
</dependency>
26+
```
27+
28+
> [!Tip]
29+
> Latest version can be found on this [page](https://mvnrepository.com/artifact/com.azure/azure-identity)
30+
31+
See [Azure authentication with Java and Azure Identity](/azure/developer/java/sdk/identity) to learn more.
32+
33+
- Add [azure-messaging-webpubsub](https://mvnrepository.com/artifact/com.azure/azure-messaging-webpubsub) dependency in your `pom.xml`.
34+
35+
```xml
36+
<dependency>
37+
<groupId>com.azure</groupId>
38+
<artifactId>azure-messaging-webpubsub</artifactId>
39+
<version>1.0.0-beta.6</version>
40+
</dependency>
41+
```
42+
43+
> [!Tip]
44+
> Latest version can be found on this [page](https://mvnrepository.com/artifact/com.azure/azure-messaging-webpubsub)
45+
46+
## Sample codes
47+
48+
1. Create a `TokenCredential` with Azure Identity SDK.
49+
50+
```java
51+
package com.webpubsub.tutorial;
52+
53+
import com.azure.core.credential.TokenCredential;
54+
import com.azure.identity.DefaultAzureCredentialBuilder;
55+
56+
public class App {
57+
58+
public static void main(String[] args) {
59+
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
60+
}
61+
}
62+
```
63+
64+
`credential` can be any class that inherits from `TokenCredential` class.
65+
66+
- EnvironmentCredential
67+
- ClientSecretCredential
68+
- ClientCertificateCredential
69+
- ManagedIdentityCredential
70+
- VisualStudioCredential
71+
- VisualStudioCodeCredential
72+
- AzureCliCredential
73+
74+
To learn more, see [Azure Identity client library for Java](/java/api/overview/azure/identity-readme)
75+
76+
2. Then create a `client` with `endpoint`, `hub`, and `credential`.
77+
78+
```Java
79+
package com.webpubsub.tutorial;
80+
81+
import com.azure.core.credential.TokenCredential;
82+
import com.azure.identity.DefaultAzureCredentialBuilder;
83+
import com.azure.messaging.webpubsub.WebPubSubServiceClient;
84+
import com.azure.messaging.webpubsub.WebPubSubServiceClientBuilder;
85+
86+
public class App {
87+
public static void main(String[] args) {
88+
89+
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
90+
91+
// create the service client
92+
WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
93+
.endpoint("<endpoint>")
94+
.credential(credential)
95+
.hub("<hub>")
96+
.buildClient();
97+
}
98+
}
99+
```
100+
101+
Learn how to use this client, see [Azure Web PubSub service client library for Java](/java/api/overview/azure/messaging-webpubsub-readme)
102+
103+
## Complete sample
104+
105+
- [Simple chatroom with AAD Auth](https://github.com/Azure/azure-webpubsub/tree/main/samples/java/chatapp-aad)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: How to create a WebPubSubServiceClient with JavaScript and Azure Identity
3+
description: How to create a WebPubSubServiceClient with JavaScript and Azure Identity
4+
author: terencefan
5+
6+
ms.author: tefa
7+
ms.date: 11/15/2021
8+
ms.service: azure-web-pubsub
9+
ms.topic: how-to
10+
---
11+
12+
# How to create a `WebPubSubServiceClient` with JavaScript and Azure Identity
13+
14+
This how-to guide shows you how to create a `WebPubSubServiceClient` using Azure Active Directory in JavaScript.
15+
16+
## Requirements
17+
18+
- Install [@azure/identity](https://www.npmjs.com/package/@azure/identity) package from npmjs.com.
19+
20+
```bash
21+
npm install --save @azure/identity
22+
```
23+
24+
- Install [@azure/web-pubsub](https://www.npmjs.com/package/@azure/web-pubsub) package from npmjs.com
25+
26+
```bash
27+
npm install @azure/web-pubsub
28+
```
29+
30+
## Sample codes
31+
32+
1. Create a `TokenCredential` with Azure Identity SDK.
33+
34+
```javascript
35+
const { DefaultAzureCredential } = require('@azure/identity')
36+
37+
let credential = new DefaultAzureCredential();
38+
```
39+
40+
`credential` can be any class that inherits from `TokenCredential` class.
41+
42+
- EnvironmentCredential
43+
- ClientSecretCredential
44+
- ClientCertificateCredential
45+
- ManagedIdentityCredential
46+
- VisualStudioCredential
47+
- VisualStudioCodeCredential
48+
- AzureCliCredential
49+
50+
To learn more, see [Azure Identity client library for JavaScript](/javascript/api/overview/azure/identity-readme)
51+
52+
2. Then create a `client` with `endpoint`, `hub`, and `credential`.
53+
54+
```javascript
55+
const { DefaultAzureCredential } = require('@azure/identity')
56+
57+
let credential = new DefaultAzureCredential();
58+
59+
let serviceClient = new WebPubSubServiceClient("<endpoint>", credential, "<hub>");
60+
```
61+
62+
Learn how to use this client, see [Azure Web PubSub service client library for JavaScript](/javascript/api/overview/azure/web-pubsub-readme)
63+
64+
## Complete sample
65+
66+
- [Simple chatroom with AAD Auth](https://github.com/Azure/azure-webpubsub/tree/main/samples/javascript/chatapp-aad)

0 commit comments

Comments
 (0)