Skip to content

Commit a87f84c

Browse files
authored
Update windows-uwp-csharp-interactive.md (#10542)
* Update windows-uwp-csharp-interactive.md * Update interactive.md
1 parent 02bfc33 commit a87f84c

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

articles/quickstart/native/windows-uwp-csharp/interactive.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,103 @@ locale: en-US
1111

1212
# Add Login to Your UWP application
1313

14-
1514
<p>This tutorial demonstrates how to add user login to a UWP C# application using Auth0. We recommend that you log in to follow this quickstart with examples configured for your account.</p><h2>System Requirements</h2><p>This tutorial and sample project have been tested with the following:</p><ul><li><p>Microsoft Visual Studio 2022</p></li><li><p>Windows 10 SDK (10.0.26100.0)</p></li><li><p>Auth0.OidcClient.UWP 4.0.0</p></li></ul><div></div><p></p>
1615

16+
# Configure Auth0
17+
18+
<%= include('../_includes/_getting_started', { library: 'Windows Universal' }) %>
19+
20+
<%= include('../../../_includes/_callback_url') %>
21+
22+
::: note
23+
If you are following along with the sample project you downloaded from the top of this page, you should set the **Allowed Callback URLs** to `https://${account.namespace}/mobile`.
24+
:::
25+
26+
<%= include('../../../_includes/_logout_url', { returnTo: 'https://' + account.namespace + '/mobile' }) %>
27+
28+
# Integrate Auth0 in your Application
29+
30+
## Install Dependencies
31+
32+
Use the NuGet Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console) to install the `Auth0.OidcClient.UWP` package, running the command:
33+
34+
${snippet(meta.snippets.dependencies)}
35+
36+
## Trigger Authentication {{{ data-action="code" data-code="MainPage.xaml.cs" }}}
37+
38+
To integrate Auth0 login into your application, simply instantiate an instance of the `Auth0Client` class, configuring the Auth0 Domain and Client ID:
39+
40+
${snippet(meta.snippets.setup)}
41+
42+
You can then call the `LoginAsync` method to log the user in:
43+
44+
${snippet(meta.snippets.use)}
45+
46+
![](/media/articles/native-platforms/windows-uwp-csharp/universal-login.png)
47+
48+
This loads the Auth0 login page into a web view. You can learn how to customize the login page at <a href="/universal-login#simple-customization" target="_blank" rel="noreferrer">this document</a>.
49+
50+
## Handle Authentication Tokens
51+
52+
The returned login result indicates whether authentication was successful, and if so contains the tokens and claims of the user.
53+
54+
### Authentication Error
55+
56+
You can check the `IsError` property of the result to see whether the login has failed. The `ErrorMessage` contains more information regarding the error which occurred.
57+
58+
```csharp
59+
if (loginResult.IsError)
60+
{
61+
Debug.WriteLine($"An error occurred during login: {loginResult.Error}")
62+
}
63+
```
64+
65+
### Accessing the tokens
66+
67+
On successful login, the login result contains the ID Token and Access Token in the `IdentityToken` and `AccessToken` properties respectively.
68+
69+
```csharp
70+
if (!loginResult.IsError)
71+
{
72+
Debug.WriteLine($"id_token: {loginResult.IdentityToken}");
73+
Debug.WriteLine($"access_token: {loginResult.AccessToken}");
74+
}
75+
```
76+
77+
### Obtaining the User Information
78+
79+
On successful login, the login result contains the user information in the `User` property, which is a <a href="https://msdn.microsoft.com/en-us/library/system.security.claims.claimsprincipal(v=vs.110).aspx" target="_blank" rel="noreferrer">ClaimsPrincipal</a>.
80+
81+
To obtain information about the user, you can query the claims. You can for example obtain the user's name and email address from the `name` and `email` claims:
82+
83+
```csharp
84+
if (!loginResult.IsError)
85+
{
86+
Debug.WriteLine($"name: {loginResult.User.FindFirst(c => c.Type == "name")?.Value}");
87+
Debug.WriteLine($"email: {loginResult.User.FindFirst(c => c.Type == "email")?.Value}");
88+
}
89+
```
90+
91+
::: note
92+
The exact claims returned depends on the scopes that were requested. For more information see @scopes.
93+
:::
94+
95+
You can obtain a list of all the claims contained in the ID Token by iterating through the `Claims` collection:
96+
97+
```csharp
98+
if (!loginResult.IsError)
99+
{
100+
foreach (var claim in loginResult.User.Claims)
101+
{
102+
Debug.WriteLine($"{claim.Type} = {claim.Value}");
103+
}
104+
}
105+
```
106+
107+
## Logout
108+
109+
To log the user out call the `LogoutAsync` method.
110+
111+
```csharp
112+
await client.LogoutAsync();
113+
```

0 commit comments

Comments
 (0)