-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (131 loc) · 5.4 KB
/
Copy pathProgram.cs
File metadata and controls
147 lines (131 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
namespace Hello
{
class Program
{
public class GetTokenResponse
{
public string access_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string refresh_token { get; set; }
public string id_token { get; set; }
}
static async Task Main(string[] args)
{
// This is an example application that allows the user to login into Azure AD B2C directory
// to obtain a JWT token, and then call a DNN Website that has been setup with the
// DNN Azure AD B2C Auth provider
// You need to:
// 1. Create a ROPC policy in B2C and ensure you specify the "emails" claim on the policy
// 2. Register an application
// 3. Setup the DNN portal to enable the JWT auth through the advanced settings, and add the applicationId
// to the list of valid audiences
// More info at https://docs.microsoft.com/es-es/azure/active-directory-b2c/configure-ropc
const string tenantName = "intelequiab2c";
const string policyName = "b2c_1_ropc";
const string applicationId = "5de1c393-b373-450f-9ee2-b1a945d96d77";
const string helloDnnEndpoint = "https://b2c.dnndev.me/DesktopModules/DotNetNuke.Authentication.Azure.B2C.Services/api/Hello/Test";
var tokenEndpoint = $"https://{tenantName}.b2clogin.com/{tenantName}.onmicrosoft.com/oauth2/v2.0/token?p={policyName}"
+ $"&scope=openid+{applicationId}+offline_access&client_id={applicationId}&response_type=token+id_token&grant_type=password";
var user = ReadUsername();
var password = ReadPassword();
try
{
var tokenResponse = await GetTokenAsync(user, password, tokenEndpoint);
Console.WriteLine("Login successfull. Obtaining user info from DNN instance...");
var whoAmI = await GetHello(helloDnnEndpoint, tokenResponse.access_token);
Console.WriteLine(whoAmI);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
static string ReadUsername()
{
string user;
do
{
Console.Write("User: ");
user = Console.ReadLine().Trim();
if (!string.IsNullOrEmpty(user))
{
break;
}
Console.WriteLine("Bad username, please type your username");
} while (true);
return user;
}
static string ReadPassword()
{
string pass = "";
Console.Write("Password: ");
do
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
else if (key.Key == ConsoleKey.Enter)
{
break;
}
}
} while (true);
Console.WriteLine();
return pass;
}
static async Task<GetTokenResponse> GetTokenAsync(string userName, string password, string tokenEndpoint)
{
tokenEndpoint += $"&username={HttpUtility.UrlEncode(userName)}";
tokenEndpoint += $"&password={HttpUtility.UrlEncode(password)}";
var request = new HttpRequestMessage(HttpMethod.Post, tokenEndpoint);
using (var client = new HttpClient())
{
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var getTokenResult = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetTokenResponse>(getTokenResult);
}
else
{
var errorDesc = await response.Content.ReadAsStringAsync();
throw new ApplicationException(errorDesc);
}
}
}
static async Task<string> GetHello(string endpoint, string authToken)
{
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
request.Headers.Add("Authorization", $"Bearer {authToken}");
using (var client = new HttpClient())
{
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
var errorDesc = await response.Content.ReadAsStringAsync();
throw new ApplicationException(errorDesc);
}
}
}
}
}