Skip to content

Commit 90984d4

Browse files
terencefanvicancy
authored andcommitted
how to use aad in different languages
1 parent 029b274 commit 90984d4

7 files changed

+358
-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-use-aad-in-csharp)
115+
- [Python](./howto-use-aad-in-python)
116+
- [Java](./howto-use-aad-in-java)
117+
- [JavaScript](./howto-use-aad-in-javascript)
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-use-aad-in-csharp)
100+
- [Python](./howto-use-aad-in-python)
101+
- [Java](./howto-use-aad-in-java)
102+
- [JavaScript](./howto-use-aad-in-javascript)
130103

131104
## Next steps
132105

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
title: How to use Azure Active Directory (AAD) in C#
3+
description: How to use Azure Active Directory (AAD) in C#
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 use Azure Active Directory (AAD) in C#
13+
14+
## Requirements
15+
16+
- Install [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) from nuget.org.
17+
18+
```bash
19+
Install-Package Azure.Identity
20+
```
21+
22+
- Install [Azure.Messaging.WebPubSub](https://www.nuget.org/packages/Azure.Messaging.WebPubSub) from nuget.org
23+
24+
```bash
25+
Install-Package Azure.Messaging.WebPubSub
26+
```
27+
28+
## Sample codes
29+
30+
1. Create a `TokenCredential` with Azure Identity SDK.
31+
32+
```C#
33+
using Azure.Identity
34+
35+
namespace chatapp
36+
{
37+
public class Program
38+
{
39+
public static void Main(string[] args)
40+
{
41+
var credential = new DefaultAzureCredential();
42+
}
43+
}
44+
}
45+
```
46+
47+
`credential` can be any class that inherits from `TokenCredential` class.
48+
49+
- EnvironmentCredential
50+
- ClientSecretCredential
51+
- ClientCertificateCredential
52+
- ManagedIdentityCredential
53+
- VisualStudioCredential
54+
- VisualStudioCodeCredential
55+
- AzureCliCredential
56+
- ...
57+
58+
To learn more, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme)
59+
60+
2. Then create a `client` with `endpoint`, `hub`, and `credential`.
61+
62+
```C#
63+
using Azure.Identity
64+
65+
public class Program
66+
{
67+
public static void Main(string[] args)
68+
{
69+
var credential = new DefaultAzureCredential();
70+
var client = new WebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", credential);
71+
}
72+
}
73+
```
74+
75+
Or inject it into `IServiceCollections` with our `BuilderExtensions`.
76+
77+
```C#
78+
using System;
79+
80+
using Azure.Identity;
81+
82+
using Microsoft.Extensions.Azure;
83+
using Microsoft.Extensions.Configuration;
84+
using Microsoft.Extensions.DependencyInjection;
85+
86+
namespace chatapp
87+
{
88+
public class Startup
89+
{
90+
public Startup(IConfiguration configuration)
91+
{
92+
Configuration = configuration;
93+
}
94+
95+
public IConfiguration Configuration { get; }
96+
97+
public void ConfigureServices(IServiceCollection services)
98+
{
99+
services.AddAzureClients(builder =>
100+
{
101+
var credential = new DefaultAzureCredential();
102+
builder.AddWebPubSubServiceClient(new Uri("<endpoint>"), "<hub>", credential);
103+
});
104+
}
105+
}
106+
}
107+
```
108+
109+
3. Learn how to use this client, see [Azure Web PubSub service client library for .NET](/dotnet/api/overview/azure/messaging.webpubsub-readme-pre)
110+
111+
## Complete sample
112+
113+
- [Simple chatroom with AAD Auth](https://github.com/Azure/azure-webpubsub/tree/main/samples/csharp/chatapp-aad)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: How to use Azure Active Directory (AAD) in Java
3+
description: How to use Azure Active Directory (AAD) in Java
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 use Azure Active Directory (AAD) in Java
13+
14+
## Requirements
15+
16+
- Add [azure-identity](https://mvnrepository.com/artifact/com.azure/azure-identity) dependency in your `pom.xml`.
17+
18+
```xml
19+
<dependency>
20+
<groupId>com.azure</groupId>
21+
<artifactId>azure-identity</artifactId>
22+
<version>1.4.1</version>
23+
</dependency>
24+
```
25+
26+
> Latest version can be found on this [page](https://mvnrepository.com/artifact/com.azure/azure-identity)
27+
28+
See [Azure authentication with Java and Azure Identity](/azure/developer/java/sdk/identity) to learn more.
29+
30+
- Add [azure-messaging-webpubsub]() dependency in your `pom.xml`.
31+
32+
```xml
33+
<dependency>
34+
<groupId>com.azure</groupId>
35+
<artifactId>azure-messaging-webpubsub</artifactId>
36+
<version>1.0.0-beta.2</version>
37+
</dependency>
38+
```
39+
40+
> Latest version can be found on this [page](https://mvnrepository.com/artifact/com.azure/azure-messaging-webpubsub)
41+
42+
## Sample codes
43+
44+
1. Create a `TokenCredential` with Azure Identity SDK.
45+
46+
```java
47+
package com.webpubsub.tutorial;
48+
49+
import com.azure.core.credential.TokenCredential;
50+
import com.azure.identity.DefaultAzureCredentialBuilder;
51+
52+
public class App {
53+
54+
public static void main(String[] args) {
55+
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
56+
}
57+
}
58+
```
59+
60+
`credential` can be any class that inherits from `TokenCredential` class.
61+
62+
- EnvironmentCredential
63+
- ClientSecretCredential
64+
- ClientCertificateCredential
65+
- ManagedIdentityCredential
66+
- VisualStudioCredential
67+
- VisualStudioCodeCredential
68+
- AzureCliCredential
69+
- ...
70+
71+
To learn more, see [Azure Identity client library for Java](/java/api/overview/azure/identity-readme)
72+
73+
2. Then create a `client` with `endpoint`, `hub`, and `credential`.
74+
75+
```Java
76+
package com.webpubsub.tutorial;
77+
78+
import com.azure.core.credential.TokenCredential;
79+
import com.azure.identity.DefaultAzureCredentialBuilder;
80+
import com.azure.messaging.webpubsub.WebPubSubServiceClient;
81+
import com.azure.messaging.webpubsub.WebPubSubServiceClientBuilder;
82+
83+
public class App {
84+
public static void main(String[] args) {
85+
86+
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
87+
88+
// create the service client
89+
WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
90+
.endpoint("<endpoint>")
91+
.credential(credential)
92+
.hub("<hub>")
93+
.buildClient();
94+
}
95+
}
96+
```
97+
98+
3. Learn how to use this client, see [Azure Web PubSub service client library for Java](/java/api/overview/azure/messaging-webpubsub-readme)
99+
100+
## Complete sample
101+
102+
- [Simple chatroom with AAD Auth](https://github.com/Azure/azure-webpubsub/tree/main/samples/java/chatapp-aad)

0 commit comments

Comments
 (0)