Skip to content

Commit 1670561

Browse files
committed
got auth working
2 parents 3a06f03 + 6361ced commit 1670561

File tree

2 files changed

+11
-66
lines changed

2 files changed

+11
-66
lines changed

ConverterLibrary/Constants/Constants.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,10 @@ public static class Constants
7575
}
7676
";
7777

78-
public static string AuthString = "https://login.windows.net/common";
79-
public static string ClientId = "748bbb12-57ae-4ade-8138-13cd7caa0027";
80-
// public static string ClientSecret = "NnFK4FPwE+usvvTIOvjUpmY8/zC2dXwm5i89UsSfNSg=";
81-
public static string ResourceUrl = "https://management.core.windows.net/";
82-
// public static string ResourceUrl = "http://jeffhollanlive.onmicrosoft.com/armexplorer";
83-
84-
85-
public static string RedirectUrl = "http://localhost/";
86-
public static string AuthUrl = "https://login.microsoftonline.com/common/oauth2/token";
78+
public static string AuthString = "https://login.windows.net/common/oauth2/authorize";
79+
public static string ClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
80+
public static string ResourceUrl = "https://management.core.windows.net/";
81+
public static string RedirectUrl = "urn:ietf:wg:oauth:2.0:oob";
8782
}
8883
}
8984

ConverterLibrary/Converter.cs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
using System.Net.Http.Headers;
1414
using System.Text;
1515
using System.Threading.Tasks;
16-
using System.Web;
17-
using System.Windows.Forms;
1816

1917
namespace ConverterLibrary
2018
{
@@ -44,7 +42,6 @@ public class Converter : PSCmdlet
4442
public string Token = "";
4543
private DeploymentTemplate template;
4644
private JObject workflowTemplateReference;
47-
private AuthenticationContext ac = new AuthenticationContext(Constants.AuthString);
4845

4946

5047
public Converter()
@@ -61,66 +58,19 @@ protected override void ProcessRecord()
6158
{
6259
if (String.IsNullOrEmpty(Token))
6360
{
61+
AuthenticationContext ac = new AuthenticationContext(Constants.AuthString, true);
62+
var ar = ac.AcquireToken(Constants.ResourceUrl, Constants.ClientId, new Uri(Constants.RedirectUrl), PromptBehavior.Always);
6463

65-
var uri = String.Format("https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id={0}&redirect_uri={1}&resource={2}&prompt=consent",
66-
Constants.ClientId, HttpUtility.UrlEncode(Constants.RedirectUrl), HttpUtility.UrlEncode(Constants.ResourceUrl));
67-
WriteVerbose(uri);
68-
WebBrowser browser = new WebBrowser();
69-
browser.ScrollBarsEnabled = true;
70-
browser.Width = 800;
71-
browser.Height = 840;
72-
browser.ScriptErrorsSuppressed = true;
73-
browser.Url = new Uri(uri);
74-
75-
Form form = new Form();
76-
browser.DocumentCompleted += Browser_DocumentCompleted;
77-
form.Controls.Add(browser);
78-
form.AutoSize = true;
79-
form.ShowIcon = false;
80-
form.AutoSizeMode = AutoSizeMode.GrowOnly;
81-
form.Width = 810;
82-
form.Height = 840;
83-
form.ShowDialog();
84-
85-
// Token = ar.AccessToken;
86-
}
87-
WriteVerbose("Retrieved Token");
88-
var result = ConvertWithToken(SubscriptionId, ResourceGroup, LogicApp, Token).Result;
89-
WriteObject(result.ToString());
90-
}
91-
92-
private void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
93-
{
94-
if (e.Url.AbsoluteUri.Contains("code="))
95-
{
9664

97-
var queryDictionary = System.Web.HttpUtility.ParseQueryString(e.Url.Query);
98-
var code = queryDictionary[0];
99-
WriteVerbose("fullURL: " + e.Url.AbsoluteUri);
100-
WriteVerbose("code: " + code);
65+
Token = ar.AccessToken;
10166

102-
using (var client = new HttpClient())
103-
{
104-
IEnumerable<KeyValuePair<string, string>> authorizeParameters = new[] {
105-
new KeyValuePair<string,string>("client_id",Constants.ClientId),
106-
new KeyValuePair<string,string>("code",code),
107-
new KeyValuePair<string, string>("grant_type", "authorization_code"),
108-
new KeyValuePair<string, string>("redirect_uri", Constants.RedirectUrl)
109-
};
110-
var response = client.PostAsync(Constants.AuthUrl, new FormUrlEncodedContent(authorizeParameters)).Result;
111-
WriteVerbose(response.Content.ReadAsStringAsync().Result);
112-
}
113-
((WebBrowser)sender).FindForm().Close();
114-
115-
}
116-
else if (e.Url.AbsoluteUri.Contains("error="))
117-
{
118-
WriteVerbose("error: " + e.Url.AbsoluteUri);
119-
((WebBrowser)sender).FindForm().Close();
67+
WriteVerbose("Retrieved Token: " + Token);
12068
}
121-
69+
var result = ConvertWithToken(SubscriptionId, ResourceGroup, LogicApp, Token).Result;
70+
WriteObject(result.ToString());
12271
}
12372

73+
12474
public async Task<JObject> ConvertWithToken(string subscriptionId, string resourceGroup, string logicAppName, string bearerToken)
12575
{
12676
SubscriptionId = subscriptionId;

0 commit comments

Comments
 (0)